다음은 제 코드입니다. 다음과 같이 빈 배열을 반환합니다. Array ( ) 내 코드를 확인하고 변경 사항을 알려주십시오. JSON 배열을 만들고 URL에 게시하려고합니다.JSON이 빈 배열을 반환합니다. 안드로이드
String checkin = edit_message.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
String httppostURL = "http:// ...";
HttpPost httppost = new HttpPost(httppostURL);
Log.v(TAG, "postURL: " + httppost);
JSONObject data1 = new JSONObject();
data1.put("merchant_id", "02");
data1.put("merchant_location_id", "03");
data1.put("user_id", "04");
data1.put("merchant_kiosk_id", "04");
data1.put("subscriber_phone", checkin);
JSONArray jsonArray = new JSONArray();
jsonArray.put(data1);
JSONObject data= new JSONObject();
data.put("data",jsonArray);
httppost.setHeader(HTTP.CONTENT_TYPE,"application/x-www-form-urlencoded;charset=UTF-8");
//httppost.setEntity(new UrlEncodedFormEntity(data , "UTF-8"));
StringEntity se= new StringEntity(data.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v(TAG, "Response: " + responseStr);
Log.i("TAG",""+response.getStatusLine().getStatusCode());
Toast.makeText(CheckinActivity.this, responseStr, Toast.LENGTH_LONG).show();
//you can add an if statement here and do other actions based on the response
}
edit_message.setText("");
//Toast.makeText(getBaseContext(),"Sent",Toast.LENGTH_SHORT).show();
Toast.makeText(CheckinActivity.this, "Data: " +data,Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable t) {
Toast.makeText(CheckinActivity.this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}
}
UPDATE :
대신
JSONObject data= new JSONObject();
data.put("data",jsonArray);
내가 사용 :
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("data", data1.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
업데이트 2
나는이
같은 JSON 응답이{"msg":Successfully logged in,"status":1}
status = 1 인 경우 status = 0 인 경우 일부 작업을 수행하고 싶습니다. 다른 작업을 수행하고 싶습니다. 이것을 달성하는 방법? 그리고 코드를 어디에 넣어야합니까? 미리 감사드립니다.
어레이() '얻는 경우는'응답 문제는 서버 측에서 –
<의 NameValuePair> nvps = 새로운 ArrayList를 <의 NameValuePair>()에 대한 API 인 수단; \t nvps.add (새 BasicNameValuePair ("data", data.toString())); \t httppost.setEntity (새 UrlEncodedFormEntity (nvps, HTTP.UTF_8)); – Koyel
나는 다음과 같은 json 응답을 가지고있다 { "msg": 성공적으로 "status": 1} 상태가 1이면, 어떤 활동을하고 싶다. 상태 = 0이면 다른 일을하고 싶다. 이것을 달성하는 방법? 그리고 코드를 어디에 넣어야합니까? 미리 감사드립니다. – Koyel