现在我想添加一些数据到服务器使用Http请求,当我添加这些数据与hardCoded的值,它的工作将给我一个响应码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();
}
}请求类的代码
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的输出

那么,我如何修复这个错误,并证明Postman工作得很好呢?
发布于 2018-07-04 15:02:12
500内部服务器错误表示服务器在响应您的请求时出现问题。您得不到JSON字符串响应。
请再次检查您的请求。检查您传递的所有内容,如标头、参数。将您的请求与正在工作的邮递员请求进行比较。一定是缺少某些参数。这会给服务器的响应带来麻烦。
https://stackoverflow.com/questions/51167340
复制相似问题