Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 返回空数组的JSON。安卓_Android_Arrays_Json_Parsing_Post - Fatal编程技术网

Android 返回空数组的JSON。安卓

Android 返回空数组的JSON。安卓,android,arrays,json,parsing,post,Android,Arrays,Json,Parsing,Post,下面是我的代码。它返回空数组,如:array ( ) 请检查我下面的代码,让我知道要做什么更改。我正在创建一个JSON数组,并希望将其发布到url String checkin = edit_message.getText().toString(); HttpClient httpclient = new DefaultHttpClient(); String httppostURL = "h

下面是我的代码。它返回空数组,如:array ( ) 请检查我下面的代码,让我知道要做什么更改。我正在创建一个JSON数组,并希望将其发布到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();
                 }

            }

    }
更新:

而不是

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

如果status=1,我想做一些活动&如果status=0,我想做其他事情。如何做到这一点?代码放在哪里?提前谢谢

使用JSON类进行解析,例如
   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();
                 }

            }

    }
您可以使用下面的代码进行解析

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

这就是你想要的吗??请更新问题标题以获得您所需的内容。

如果得到
Array()
的响应,则表示问题在服务器侧列表的api中nvps=new ArrayList();添加(新的BasicNameValuePair(“data”,data.toString());setEntity(新的UrlEncodedFormEntity(nvps,HTTP.UTF_8));我有这样一个json响应{“msg”:成功登录,“status”:1}如果status=1,我想做一些活动&如果status=0,我想做其他事情。如何做到这一点?代码放在哪里?提前谢谢。但它不起作用。我做到了:List nvps=newarraylist();添加(新的BasicNameValuePair(“data”,data.toString());setEntity(新的UrlEncodedFormEntity(nvps,HTTP.UTF_8));我有这样一个json响应{“msg”:成功登录,“status”:1}如果status=1,我想做一些活动&如果status=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
}
}
    }
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
}
}
    }