0

Now I want to add some data to the server using Http Request when i add this data with hardCoded value it works will and give me a Response code 200 but when it come from the user it comes with 500 response code and error The code of http request helper class.

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();
}


  }

The code for the Request class

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();

    }

}

The Output from the logcat

enter image description here

So How can i fix this error and put in fact that Postman works well .

1 Answer 1

2

A 500 Internal Server error means the server had a problem responding to your request. You are not getting a JSON string response.

Please check your request again. Check everything the headers, parameters that you pass. Compare your request with the working postman request. There must be some parameter missing. Which causing trouble to the server to respond you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.