2012-08-22 6 views
0

json : fileName 및 json 문자열을 저장하기위한 2 개의 매개 변수를 허용하는 웹 서비스가 있습니다. 이 웹 서비스에 json 문자열을 게시해야합니다. How to send a JSON object over Request with Android?에 설명 된 방법을 시도했지만 작동하지 않습니다. 모든 포인터 ??안드로이드 - 매개 변수를 받아들이는 웹 서비스에 json 문자열을 게시하는 방법?

public void postDataToServer(String url, String jsonStr) throws ClientProtocolException, IOException 
    { 
     int TIMEOUT_MILLISEC = 10000; // = 10 seconds 
     HttpParams httpParams = new BasicHttpParams(); 
     HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); 
     HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); 
     httpParams.setParameter("fileName","testFile"); 
     httpParams.setParameter("json",jsonStr); 
     HttpClient client = new DefaultHttpClient(httpParams); 

     HttpPost request = new HttpPost(url); 
     request.setEntity(new ByteArrayEntity(
      jsonStr.getBytes("UTF8"))); 
     HttpResponse response = client.execute(request); 

    } 
+1

는 "작동하지 않는 것"을. 그리고 그게 무슨 뜻일까요? – njzk2

+0

서버에서 json을 검색하려고하면 "Json 파일을 찾을 수 없음"오류가 발생합니다. – Srikanth

답변

0

fileName과 json은 httpParams에 포함되지 않습니다. 그들은 엔티티에 들어갑니다. HttpEntity를 사용해야하며, 가장 일반적인 경우는 http://developer.android.com/reference/org/apache/http/client/entity/UrlEncodedFormEntity.html 이며 2 개의 BasicNameValuePair, 하나는 fileName, 하나는 json입니다.

+0

감사합니다. 샘플 코드는 다음과 같습니다. http://stackoverflow.com/questions/11417888/how-do-i-post-a-parameter-to-a-web-service-from-android – Srikanth

0

이 내 사용자 정의 WebServiceHelper 클래스입니다 :

public class WebserviceHelper { 


    private Context c; 
    public String bytesSent; 
    private String tempRespo; 

    public String hitWeb(Context c,String url, String json){ 

     this.c=c; 
     try { 
      tempRespo = new hitAsync(url,json).execute().get(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return tempRespo; 
    } 

    class hitAsync extends AsyncTask<Void, Void , String> 
    { 


     private String url; 
     private String json; 
     ProgressDialog pDialog; 

     public hitAsync(String url, String json) { 
      // TODO Auto-generated constructor stub 

      this.url = url; 
      this.json = json; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
      pDialog.dismiss(); 
     } 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 

      pDialog = new ProgressDialog(c); 
      pDialog.setMessage("Please Wait..."); 
      pDialog.show(); 
      pDialog.setCancelable(false); 

     } 

     @Override 
     protected String doInBackground(Void... params) { 
      // TODO Auto-generated method stub 

       HttpClient client = new DefaultHttpClient(); 
       HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit 
       HttpResponse response; 

       try{ 
        HttpPost post = new HttpPost(url); 

        StringEntity se = new StringEntity(json); 
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
        post.setEntity(se); 
        response = client.execute(post); 
        /*Checking response */ 
        if(response!=null){ 
         InputStream in = response.getEntity().getContent(); //Get the data in the entity 

         BufferedInputStream bis = new BufferedInputStream(in); 
        ByteArrayBuffer baf = new ByteArrayBuffer(20); 

        int current = 0; 
        while ((current = bis.read()) != -1) { 
         baf.append((byte) current); 
        } 

        bytesSent = new String(baf.toByteArray()); 

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


     } 

    } 

는 다음 클래스의 객체를 만들고 당신의 일을 수행

ws = new WebserviceHelper(); 
    String respo = ws.hitWeb(// ur class context", "// url","//json string"); 
관련 문제