홈>
이제 hardCoded 값 으로이 데이터를 추가 할 때 Http Request를 사용하여 서버에 일부 데이터를 추가하고 싶습니다. 작동 코드는 200이고 응답 코드는 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();
}
}
로그 캣의 출력
그래서이 오류를 어떻게 수정하고 실제로 우편 배달부 잘 작동합니다.
-
답변 # 1
트렌드
- OpenCv의 폴더에서 여러 이미지 읽기 (python)
- 파이썬 셀레늄 모든 "href"속성 가져 오기
- git commit - 자식 - 로컬 커밋 된 파일에 대한 변경을 취소하는 방법
- html - 자바 스크립트 - 클릭 후 변경 버튼 텍스트 변경
- JSP에 대한 클래스를 컴파일 할 수 없습니다
- javascript - 현재 URL에서 특정 div 만 새로 고침/새로 고침
- jquery - JavaScript로 현재 세션 값을 얻으시겠습니까?
- javascript - swiperjs에서 정지, 재생 버튼 추가
- vue.js - axios를 사용하여 서버에 이미지를 업로드하는 방법
- python - 문자열에서 특정 문자 제거
500 내부 서버오류는 서버가 요청에 응답하는 데 문제가 있음을 의미합니다. JSON 문자열 응답이 없습니다.
요청을 다시 확인하십시오. 전달하는 헤더, 매개 변수를 모두 확인하십시오. 귀하의 요청을 우편 배달부 요청과 비교하십시오. 누락 된 매개 변수가 있어야합니다. 서버에서 응답하지 않는 문제가 있습니다.