2015-02-03 2 views
0

다음은 제 코드입니다. 다음과 같이 빈 배열을 반환합니다. 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 인 경우 일부 작업을 수행하고 싶습니다. 다른 작업을 수행하고 싶습니다. 이것을 달성하는 방법? 그리고 코드를 어디에 넣어야합니까? 미리 감사드립니다.

+0

어레이() '얻는 경우는'응답 문제는 서버 측에서 –

+0

<의 NameValuePair> nvps = 새로운 ArrayList를 <의 NameValuePair>()에 대한 API 인 수단; \t nvps.add (새 BasicNameValuePair ("data", data.toString())); \t httppost.setEntity (새 UrlEncodedFormEntity (nvps, HTTP.UTF_8)); – Koyel

+0

나는 다음과 같은 json 응답을 가지고있다 { "msg": 성공적으로 "status": 1} 상태가 1이면, 어떤 활동을하고 싶다. 상태 = 0이면 다른 일을하고 싶다. 이것을 달성하는 방법? 그리고 코드를 어디에 넣어야합니까? 미리 감사드립니다. – Koyel

답변

0

구문 분석에 JSON 클래스 사용 예 : 구문 분석을 위해 아래 코드를 사용할 수 있습니다.

JSONObject mainObject = new JSONObject(responseStr); 
int status = mainObject.getInt("status"); 
if(status==1){ 
    //do something 
}else{ 
    //do something else 
} 
.... 

원하는 것이 무엇입니까 ?? 질문 제목을 업데이트하여 정확히 필요한 것을 얻으십시오.

+0

또한 도움이 될 것입니다 http://stackoverflow.com/questions/8091051/how-to-parse-json-string-in-android – SwANDp

+0

안녕하세요, 감사합니다. 그것은 효과가있다! :) – Koyel

+0

@Koyel 제발 내 대답을 upvote :) – SwANDp

0
String checkin = edit_message.getText().toString(); 
       HttpClient httpclient = new DefaultHttpClient(); 
       String httppostURL = "http://192.168.0.254/nepadeals/androidweb/checkin"; 
       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("Content-type", "application/json;" + HTTP.UTF_8); 
         StringEntity se= new StringEntity(data.toString(),HTTP.UTF_8); 

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

      } 

    } 
+0

감사합니다. 그러나 그것은 효과가 없었습니다. 나는했다 : List nvps = 새로운 ArrayList (); \t nvps.add (새 BasicNameValuePair ("data", data.toString())); \t httppost.setEntity (새 UrlEncodedFormEntity (nvps, HTTP.UTF_8)); – Koyel

+0

다음과 같은 json 응답이 있습니다 { "msg": "status": 1} 상태가 1이면 일부 작업을 수행하고 status = 0이면 다른 작업을 수행하고 싶습니다. 이것을 달성하는 방법? 그리고 코드를 어디에 넣어야합니까? 미리 감사드립니다. – Koyel

0
String checkin = edit_message.getText().toString(); 
       HttpClient httpclient = new DefaultHttpClient(); 
       String httppostURL = "http://192.168.0.254/nepadeals/androidweb/checkin"; 
       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("Content-type", "application/json;" + HTTP.UTF_8); 
         StringEntity se= new StringEntity(data.toString(),HTTP.UTF_8); 

         httppost.setEntity(se);     
         HttpResponse response = httpclient.execute(httppost); 
         HttpEntity entity = response.getEntity(); 
       InputStream content = entity.getContent(); 
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(content, HTTP.UTF_8)); 
       String line; 
       StringBuilder builder = new StringBuilder(); 
       while ((line = reader.readLine()) != null) { 
        builder.append(line); 
       } 

       result = builder.toString(); 

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

      } 
protected void onPostExecute(Void data) { 
     super.onPostExecute(data); 
JSONObject object1 = new JSONObject(result);   
String status=object1.getString("status"); 
int value=0; 
if(status!=null) 
{ 
value=Integer.parseInt(status); 
} 
if(value==1) 
{ 
//do some task 
} 
else 
{ 
//do some task 
} 
} 
    } 
+0

고마워요! 나는 시도 할 것이다! :) – Koyel

관련 문제