2011-10-24 4 views
2

나는 내가 안드로이드 응용 프로그램에서 모방하려고 자바 스크립트 코드가 : 여기안드로이드 포스트 요청

는 자바 스크립트 코드입니다 : 여기

text = '{"username":"Hello","password":"World"}'; 
x.open("POST", url); 
x.setRequestHeader("Content-type", "application/json"); 
x.setRequestHeader("Content-length", text.length); 
x.send(text); 

나는 안드로이드 응용 프로그램에 대한 지금까지 무엇을한다 (doesnt work) :

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

이클립스에서이 코드를 디버깅하려고하면 디버거가 멈추는 동안 에뮬레이터가 계속 실행됩니다. 감사!

참고 : httpclient.execute (httppost)에 미치는 매달려

+0

어떤 행이 걸려 있는지 알 수 있습니까? –

+0

@KurtisNusbaum 두 번째 발췌 문장 마지막 줄 –

+0

에뮬레이터의 인터넷 연결을 확인 했습니까? –

답변

3

처럼 뭔가

HttpPost httppost = new HttpPost("path"); 

나는 안드로이드 포스트 요청을 사용하는 코드입니다 :이 줄을 수정해야합니다 .

+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

URL이 유효하고 작업중인 컴퓨터에서 작동하는지 확인했습니다. –

+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); 
    } 
} 
관련 문제