2011-10-24 11 views
2

:ここAndroidのポスト要求

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

、ここでは、私はアンドロイドのアプリケーションのために、これまで持っているものです

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url);     
httppost.setHeader("Content-type", "application/json"); 
String text = "\"{\"username\":\"Hello\",\"password\":\"World\"}\""; 
httppost.setHeader("Content-length",Integer.toString(text.length())); 
httppost.setEntity(new StringEntity(text)); 
HttpResponse response = httpclient.execute(httppost); 

このコードをeclipseでデバッグしようとすると、エミュレータはデバッガがハングしている間も実行を継続します。ありがとう!

注:httpclient.execute上のハング(httppost)

+0

あなたは何行目に掛かっているのかを教えてください。 –

+0

@KurtisNusbaum 2番目のスニペットの最後の行 –

+0

インターネットへのエミュレータ接続を確認しましたか? –

答えて

3

のようなものに

HttpPost httppost = new HttpPost("path"); 

私はAndroidのポスト要求に使用コードをされて:あなたは、この行を変更する必要があります。

+0

あなたが提供したコードでのJSON要求は、ヘッダを追加しないでください。 –

0

あなただけpathにごHttpPostパスを設定するもしかして。私はあなたがHttpPostに有効なURLを与えていないのであなたの吊り下げを考える。その上

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

...と:ここでは

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

私はURLを単に表示目的のためにパスに置き換えました。それは私のsoucrコードではありません –

+0

私は、あなたの仕事をしているマシンから有効であり、有効であることを確認しましたか? –

+0

そのURLではなく、それは私のマシンから動作します –

3

はそれを試してみてください:

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

をあなたはJSのバージョンに比べて、あなたのテキスト文字列の先頭と末尾の中の余分なスピーチマークを持っていますか?

0
// 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); 
    } 
}