Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.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
Java Android post请求_Java_Javascript_Android_Http_Post - Fatal编程技术网

Java Android post请求

Java Android post请求,java,javascript,android,http,post,Java,Javascript,Android,Http,Post,我尝试在android应用程序中模仿javascript代码: 以下是javascript代码: text = '{"username":"Hello","password":"World"}'; x.open("POST", url); x.setRequestHeader("Content-type", "application/json"); x.setRequestHeader("Content-length", text.length); x.send(text); 以下是我到目前为

我尝试在android应用程序中模仿javascript代码:

以下是javascript代码:

text = '{"username":"Hello","password":"World"}';
x.open("POST", url);
x.setRequestHeader("Content-type", "application/json");
x.setRequestHeader("Content-length", text.length);
x.send(text);
以下是我到目前为止为android应用程序所做的工作(不起作用):

当我尝试在eclipse上调试此代码时,模拟器会在调试器挂起时保持运行。谢谢


注意:它挂在httpclient.execute(httppost)

上。您的意思是将httppost路径设置为仅
路径
。我认为你的绞刑是因为你没有给HttpPost一个有效的URL。您需要修改此行:

HttpPost httppost = new HttpPost("path"); 
差不多

HttpPost httppost = new HttpPost("actual/url/path"); 

以下是我用于Android post请求的代码:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("fullurl");

List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("parameter", "variable");
post.setEntity (new UrlEncodedFormEntity(pairs));

HttpResponse response = client.execute(post);
HttpClient=newdefaulthttpclient();
HttpPost=新的HttpPost(“完整URL”);
列表对=新的ArrayList();
添加(新的BasicNameValuePair(“参数”、“变量”);
post.setEntity(新的UrlEncodedFormEntity(对));
HttpResponse response=client.execute(post);
…等等。

试试看:

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url);  
JSONObject json = new JSONObject();
try{
      json.put("username", "Hello");
      json.put("password", "World");
      StringEntity se = new StringEntity(json.toString());  
      se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
      post.setEntity(se);
      HttpResponse response = httpclient.execute(httppost); 
      /*Checking response */
      if(response!=null){
          InputStream in = response.getEntity().getContent(); //Get the data in the entity

}
catch(Exception e){
    e.printStackTrace();
}

与JS版本相比,文本字符串的开头和结尾有额外的语音标记?

//创建新的HttpClient和Post头
 // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(StringUrl);

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));



        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        System.out.println("rep => " + response);


    } catch (IOException e) {
       System.out.println(e);
    }
} 
HttpClient HttpClient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(StringUrl); 试一试{ //添加您的数据 List nameValuePairs=新的ArrayList(2); 添加(新的BasicNameValuePair(“id”,“12345”); 添加(新的BasicNameValuePair(“stringdata”、“Hi”); setEntity(新的UrlEncodedFormEntity(nameValuePairs)); //执行HTTP Post请求 HttpResponse response=httpclient.execute(httppost); System.out.println(“rep=>”+响应); }捕获(IOE异常){ 系统输出打印ln(e); } }
你能告诉我它挂在哪一行吗?@KurtisNusbaum第二段的最后一行你验证了模拟器与互联网的连接了吗?@Mike G没有设置“内容长度”标题…将通过StringEntity为您完成此操作。我将url替换为路径仅用于显示目的。我知道它不在我的源代码中,您是否已验证url是否有效并在您正在使用的机器上工作?在您提供的代码中,它的JSON请求不添加标题。
 // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(StringUrl);

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));



        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        System.out.println("rep => " + response);


    } catch (IOException e) {
       System.out.println(e);
    }
}