2013-06-22 4 views
0

나는 좌절감을 느낍니다. 문제가 생겼어. REST 요청을 서버에 보내 사용자의 모든 입력을 보내려고합니다. 지금Android : 나머지 PUT - 서버에서 데이터를 보내는 방법

public class MyActivity extends Activity{ 

    private class PutServiceTask extends AsyncTask<String, Integer, String> { 


     @Override 
     protected void onPreExecute() { 
      //show Dialogbox 
     } 

     @Override 
     protected String doInBackground(String... urls) { 
      String url = urls[0]; 
      String result = ""; 
      HttpResponse response = doResponse(url); 

      if (response == null) { 
       return result; 
      } 

      return result; 

      private HttpResponse doResponse(String url) { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPut httpput = new HttpPut(url); 
       HttpResponse response = null; 

       // Add parameters 
       try { 
        response = httpclient.execute(httpput); 
        StatusLine statusLine = response.getStatusLine(); 
        int statusCode = statusLine.getStatusCode(); 
        if(statusCode == HttpStatus.SC_OK){ //200 
         httpput.addHeader("content-type", "application/json"); 
         StringEntity se = new StringEntity(params.toString()); 
         httpput.setEntity(se); 
         HttpEntity entity = response.getEntity(); 
        } 
        else { 
         System.out.println(response.getStatusLine().getReasonPhrase()); 
        } 

       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 


       return response; 
      } 
     } 

    } 
} 

을 그리고 질문 :

{ 
    id: "123", 
    text: "My Name is Peter..., 
    age": 15, 
    name: "Peter", 
    hobbies: [     
      id": 321, 
      hobbie_id": 1, 
      name": "Football", 
      },    
      id": 213, 
      hobbie_id": 1, 
      name": "Basketball", 
      }  
    ], 
    gender: true, 
    version: 1 
} 

내가 백그라운드에서 서버 통신을 처리하기 위해 AsyncTask를에서 확장하는 내부 클래스와 클래스가 :이 요청 JSON 객체가 서버에보고하는 방법이다 다른 것은 t이다

private String ageString; //age 
private String nameString; //name of the Person 
private String hobbyname; //name of the Hobby 
private String textString; //text 

다음 onPostExecute Methode.These의 서버에 넣어 내가 즉 글고에서 얻을 내 사용자 입력을하고 toString을 변환하는 방법입니다 그는 ID입니다. 서버가 ID를 생성하는 것이 맞습니까?

이 경우 누군가가 나를 도울 수 있기를 바랍니다.

미리 감사드립니다.

답변

0

이것은 너무 복잡하여 직접 처리 할 수 ​​없으므로 훨씬 더 안전한 API 및 유형 안전 데이터로이 라이브러리를 살펴보십시오. 여기

https://github.com/kodart/Httpzoid

은 간단한 사용 예를 콜백

Http http = HttpFactory.create(context); 
http.post("http://example.com/users") 
    .data(new User("John")) 
    .handler(new ResponseHandler<Void>() { 
     @Override 
     public void success(Void ignore, HttpResponse response) { 
     } 

     @Override 
     public void error(String message, HttpResponse response) { 
     } 

     @Override 
     public void failure(NetworkError error) { 
     } 

     @Override 
     public void complete() { 
     } 
    }).execute(); 

그것은 새로운 신선한이지만, 매우 유망한 보이는와

Http http = HttpFactory.create(context); 
http.post("http://example.com/users") 
    .data(new User("John")) 
    .execute(); 

또는 더 복잡하다.

관련 문제