【问题标题】:HttpConnection Error 500HttpConnection 错误 500
【发布时间】:2018-12-12 12:30:42
【问题描述】:

现在我想使用 Http Request 向服务器添加一些数据,当我使用硬编码值添加这些数据时,它会起作用并给我一个响应代码 200,但是当它来自用户时,它带有 500 响应代码和错误http请求助手类的代码。

public class JsonReader2 {

private String url;

private List<NameValuePair> pairs;

public JsonReader2(String url, List<NameValuePair> pairs) {
    this.url = url;
    this.pairs = pairs;
}

public JsonReader2(String url) {
    this.url = url;
}

public String sendRequest() {
    String response;


    try {
        // create HTTPURLConnection
        URL url = new URL(this.url);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // set connection properties
        connection.setReadTimeout(10000);
        connection.setConnectTimeout(15000);
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.setDoOutput(false);

        // set value
        OutputStream outputStream = connection.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        writer.write(getQuery(pairs));
        writer.flush();
        writer.close();
        outputStream.close();

        // then connect
        connection.connect();
        Log.e("url",writer.toString());
        Log.e("JsonReader",String.valueOf(connection.getResponseCode()));
        // get response from connection
        InputStream is ;

        int status = connection.getResponseCode();

        if (status != HttpURLConnection.HTTP_OK)
            is = connection.getErrorStream();
        else
            is = connection.getInputStream();


        // convert input stream to string response
        Scanner s = new Scanner(is).useDelimiter("\\A");
        response = s.hasNext() ? s.next() : "";


    } catch (Exception e) {
        e.printStackTrace();

        response = null;
    }

    return response;

}

// method for  converting  the  form of the data  to request form

private String getQuery(List<NameValuePair> params) {
    StringBuilder result = new StringBuilder();
    boolean first = true;

    try {

        for (NameValuePair pair : params) {
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
        }

    } catch (Exception e) {
    }

    return result.toString();
}


  }

Request 类的代码

class GetDataRequest extends AsyncTask<String, Boolean, Boolean> {
    @Override
    protected Boolean doInBackground(String... strings) {
        String name = strings[0];
        String id_client = strings[1];
        String map_address_client = strings[2];
        String clientlat = strings[3];
        String clientlag = strings[4];
        String map_address_reciver = strings[5];
        String reciver_lat = strings[6];
        String reciver_lag = strings[7];
        String delivery_time = strings[8];
        String space = strings[9];
        String name_receiver = strings[10];
        String phone_receiver = strings[11];
        String address_reciver = strings[12];
        String street_number_reciver = strings[13];
        String flower_number_reciver = strings[14];
        String building_number_reciver = strings[15];
        // if  you have  to send  data  to the databse
        ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("lang", String.valueOf(MainActivity.lang)));
        pairs.add(new BasicNameValuePair("name",  name));
        pairs.add(new BasicNameValuePair("id_client", id_client));
        pairs.add(new BasicNameValuePair("map_address_client", map_address_client));
        pairs.add(new BasicNameValuePair("client_lat", clientlat));
        pairs.add(new BasicNameValuePair("client_lag", clientlag));
        pairs.add(new BasicNameValuePair("map_address_reciver",map_address_reciver));
        pairs.add(new BasicNameValuePair("reciver_lat", reciver_lat));
        pairs.add(new BasicNameValuePair("reciver_lag",reciver_lag));
        pairs.add(new BasicNameValuePair("delivery_time", delivery_time));
        pairs.add(new BasicNameValuePair("space", space));
        pairs.add(new BasicNameValuePair("name_reciver", name_receiver));
        pairs.add(new BasicNameValuePair("phone_reciver", phone_receiver));
        pairs.add(new BasicNameValuePair("address_reciver", address_reciver));
        pairs.add(new BasicNameValuePair("street_number_reciver",street_number_reciver ));
        pairs.add(new BasicNameValuePair("flower_number_reciver", flower_number_reciver));
        pairs.add(new BasicNameValuePair("building_number_reciver",  "jjj"));



        com.mostafa.android.bsor3a.JsonReader2 j = new com.mostafa.android.bsor3a.JsonReader2(urluser, pairs);
        result = j.sendRequest();

        try {
            JSONObject jsonObject = new JSONObject(result);
            int messageId;
            if (jsonObject.has("message")) {
                JSONArray jsonArray = jsonObject.getJSONArray("message");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonobject = jsonArray.getJSONObject(i);
                    message = jsonobject.getString("message");
                    messageId = jsonobject.getInt("messageID");
                }
            }else if(jsonObject.has("data")){
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonobject = jsonArray.getJSONObject(i);
                    message = jsonobject.getString("message");
                    messageId = jsonobject.getInt("messageID");
                }
            }
            Toast.makeText(CompleteShippingActivity.this, ""+message, Toast.LENGTH_SHORT).show();

        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);

        progDailog.cancel();
        AlertDialog.Builder builder = new AlertDialog.Builder(CompleteShippingActivity.this);
        builder.setMessage(message)
                .setNegativeButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(CompleteShippingActivity.this, "onPostExecute", Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();

    }

}

logcat 的输出

那么我怎样才能修复这个错误并把事实上 邮递员工作得很好。

【问题讨论】:

    标签: android web-services httprequest androidhttpclient android-webservice


    【解决方案1】:

    500 Internal Server 错误表示服务器在响应您的请求时出现问题。您没有收到 JSON 字符串响应。

    请再次检查您的请求。检查您传递的所有标题和参数。 将您的请求与工作邮递员的请求进行比较。一定是缺少一些参数。导致服务器无法响应您。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 2016-02-23
      相关资源
      最近更新 更多