Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.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 REST API HttpPost在帖子正文上发送空数据_Android_Rest - Fatal编程技术网

使用Android REST API HttpPost在帖子正文上发送空数据

使用Android REST API HttpPost在帖子正文上发送空数据,android,rest,Android,Rest,我试图通过HttpPost发送一些数据,但结果是空的。如果我的方法和HttpGET部件运行良好,请参见下文。但当我尝试向Post body和send添加一些数据时,并没有得到任何错误,Logcat(+另一端的restweb服务)显示Post数据为空 public void sendPhoto(View v) { final String kookojaUrlGet = "http://localhost/getData"; final String kookoj

我试图通过HttpPost发送一些数据,但结果是空的。如果我的方法和HttpGET部件运行良好,请参见下文。但当我尝试向Post body和send添加一些数据时,并没有得到任何错误,Logcat(+另一端的restweb服务)显示Post数据为空

 public void sendPhoto(View v) {
        final String kookojaUrlGet = "http://localhost/getData";
        final String kookojaUrlPost = "http://localhost/postData";
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClient httpClient = new DefaultHttpClient();
                try {
                    HttpResponse getResponse = httpClient.execute(new HttpGet(kookojaUrlGet));
                    String returnedStuff = EntityUtils.toString(getResponse.getEntity());
                    Log.d("debug","http get returned " + returnedStuff);
                    JSONObject jsonGet = new JSONObject(returnedStuff);
                    int xxxTime = jsonGet.getInt("timestamp");
                    String xxxNone = jsonGet.getString("nonce");
                    String xxxApp = jsonGet.getString("appName");
                    String xxxCID = jsonGet.getString("csrfToken");
                    Log.d("debug","json was " + xxxTime + " " + xxxNone + " " + xxxApp + " " + xxxCID);
                    JSONObject c = new JSONObject();
                    c.put("_username","xxx@gmail.com");
                    c.put("_password","123");
                    c.put("_timestamp", xxxTime);
                    c.put("_nonce", xxxNone);
                    c.put("_appName", xxxApp);
                    c.put("CID", xxxCID);
                    c.put("body", photoPost);
                    c.put("long", photoLong);
                    c.put("lat", photoLat);
                    StringEntity sendStuff = new StringEntity(c.toString());
                    HttpPost xxxPost = new HttpPost(kookojaUrlPost);
                    xxxPost.setHeader("Accept", "application/json");
                    xxxPost.setHeader("Content-type", "application/json");
                    xxxPost.setHeader("Accept-Encoding", "gzip");
                    xxxPost.setEntity(sendStuff);
                    HttpResponse postResponse = httpClient.execute(xxxPost);
                    String postText = EntityUtils.toString(postResponse.getEntity());
                    Log.d("debug", "rest post response was " + postText);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

任何帮助都将不胜感激。

我用“列表”代替JSONObject解决了这个问题。代码如下:

List<NameValuePair> c = new ArrayList<NameValuePair>(2);
c.add(new BasicNameValuePair("_username", "xxx@gmail.com"));
c.add(new BasicNameValuePair("_password", "123"));
c.add(new BasicNameValuePair("_timestamp", xxxTime2));
c.add(new BasicNameValuePair("_nonce", xxxNone));
c.add(new BasicNameValuePair("_appName", xxxApp));
c.add(new BasicNameValuePair("CID", xxxCID));
c.add(new BasicNameValuePair("body", photoPost));
c.add(new BasicNameValuePair("long", "4524524524"));
c.add(new BasicNameValuePair("lat", "242452452"));
xxxPost.setEntity(new UrlEncodedFormEntity(c));
List c=newarraylist(2);
c、 添加(新的BasicNameValuePair(“\u用户名”)xxx@gmail.com"));
c、 添加(新的BasicNameValuePair(“_密码”,“123”);
c、 添加(新的BasicNameValuePair(“_timestamp”,xxxTime2));
c、 添加(新的BasicNameValuePair(“_nonce”,xxxNone));
c、 添加(新的BasicNameValuePair(“_appName”,xxxApp));
c、 添加(新的BasicNameValuePair(“CID”,xxxCID));
c、 添加(新的BasicNameValuePair(“主体”,photoPost));
c、 添加(新的BasicNameValuePair(“长”、“4524524”);
c、 添加(新的BasicNameValuePair(“lat”、“242452”);
xxxPost.setEntity(新的UrlEncodedFormEntity(c));

您设置了两次内容类型标题,这可能不是一个好主意。我的坏主意!我去掉了那条线。实际上,它在我的代码中被注释掉了。本地主机url不太可能工作,除非您的服务器实际位于设备AVD上,以便连接到本地主机。您需要使用url,而不是帮助您。