0

The following is my code. It is returning empty array, like: Array ( ) Please check my code below and let me know what change is to be made. I am creating an JSON array and want to post it to an url.

                 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:

Instead of

JSONObject data= new JSONObject();                         
data.put("data",jsonArray); 

I used:

List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("data", data1.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

UPDATE 2

I have a json response like this

{"msg":Successfully logged in,"status":1}

If status = 1, I want to do some activity & if status = 0, I want to do something else. How to achieve this? And where to put the code? Thanks in advance.

LW001
  • 2,452
  • 6
  • 27
  • 36
Koyel
  • 1
  • 1
  • 7
  • if getting `Array ( )` in response means problem is in api on server side – ρяσѕρєя K Feb 03 '15 at 06:22
  • List nvps = new ArrayList (); nvps.add(new BasicNameValuePair("data", data.toString())); httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); – Koyel Feb 03 '15 at 06:43
  • I have a json response like this {"msg":Successfully logged in,"status":1} If status = 1, I want to do some activity & if status = 0, I want to do something else. How to achieve this? And where to put the code? Thanks in advance. – Koyel Feb 03 '15 at 08:55

3 Answers3

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

            }

    }
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31
  • Thanks. But it didn't work. I did: List nvps = new ArrayList (); nvps.add(new BasicNameValuePair("data", data.toString())); httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); – Koyel Feb 03 '15 at 06:43
  • I have a json response like this {"msg":Successfully logged in,"status":1} If status = 1, I want to do some activity & if status = 0, I want to do something else. How to achieve this? And where to put the code? Thanks in advance. – Koyel Feb 03 '15 at 08:56
0

Use JSON classes for parsing e.g You can use below code for parsing.

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

Is this what you want?? please update the question title to get what exactly you need.

SwANDp
  • 59
  • 9
  • Also, this will be helpful http://stackoverflow.com/questions/8091051/how-to-parse-json-string-in-android – SwANDp Feb 03 '15 at 09:23
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
}
}
    }
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31