2014-03-12 193 views
0

我有两个文本框,其中1个用于用户名,另一个用于密码。 我想传递什么用户进入编辑文本使用POST方法通过http post参数传递参数

String request = "https://beta135.hamarisuraksha.com/web/webservice/HamariSurakshaMobile.asmx/getIMSafeAccountInfoOnLogon"; 
       URL url; 

       try { 
        url = new URL(request); 
        HttpURLConnection connection = (HttpURLConnection) url 
          .openConnection(); 
        connection.setDoOutput(true); 
        connection.setDoInput(true); 
        connection.setInstanceFollowRedirects(false); 
        connection.setRequestMethod("POST"); 
        connection.setRequestProperty("Connection", "Keep-Alive"); 
        connection.setRequestProperty("Content-Type", 
          "application/x-www-form-urlencoded;");// boundary="+CommonFunctions.boundary 
        connection.setUseCaches(false); 

        DataOutputStream wr = new DataOutputStream(
          connection.getOutputStream()); 
        wr.writeBytes(urlParameters); 
        wr.flush(); 
        wr.close(); 


        int responseCode = connection.getResponseCode(); 
        /* 
        * System.out.println("\nSending 'POST' request to URL : " + 
        * url); System.out.println("Post parameters : " + 
        * urlParameters); 
        */ 
        System.out.println("Response Code : " + responseCode); 

        InputStream errorstream = connection.getErrorStream(); 

        BufferedReader br = null; 
        if (errorstream == null) { 
         InputStream inputstream = connection.getInputStream(); 
         br = new BufferedReader(new InputStreamReader(inputstream)); 
        } else { 
         br = new BufferedReader(new InputStreamReader(errorstream)); 
        } 
        String response = ""; 
        String nachricht; 
        while ((nachricht = br.readLine()) != null) { 
         response += nachricht; 
        } 

        // print result 
        // System.out.println(response.toString()); 
        return response.toString(); 

       } catch (MalformedURLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        return null; 
       } catch (ProtocolException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        return null; 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        return null; 
       } 
      } 

回答

0

如果我正确地得到您的问题,您需要将参数传递到Web服务。在我的情况下,我已经实现了一种方法来获取网络服务响应,通过给出url和值作为参数。我认为这会帮助你。

public JSONObject getJSONFromUrl(JSONObject parm,String url) throws JSONException { 


     InputStream is = null; 
     JSONObject jObj = null; 
     String json = ""; 
     // Making HTTP request 
     try { 
      // defaultHttpClient 
      /*JSONObject parm = new JSONObject(); 
      parm.put("agencyId", 27); 
      parm.put("caregiverPersonId", 47);*/ 

     /* if(!(jObj.isNull("d"))){ 
       jObj=null; 
      } 
      */ 


      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

      httpPost.addHeader("Content-Type", "application/json; charset=utf-8"); 
      HttpEntity body = new StringEntity(parm.toString(), "utf8"); 
      httpPost.setEntity(body); 
      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 

      is = httpEntity.getContent();   

       /* String response = EntityUtils.toString(httpEntity); 
       Log.w("myApp", response);*/ 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

    // JSONObject jObj2 = new JSONObject(json); 
     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 

这个方法有两个参数。一个是url,另一个是我们应该发送给web服务的值。并简单地返回json对象。希望这将帮助你

编辑

通过您的用户名和密码,只需使用下面的代码

JsonParser jp = new JsonParser(); // create instance for the jsonparse class 

    String caregiverID = MainActivity.confirm.toString(); 

    JSONObject param = new JSONObject(); 
    JSONObject job = new JSONObject(); 
    try { 
     param.put("username", yourUserNAme); 
     job = jp.getJSONFromUrl(param, yourURL); 
+0

srry先生,我不明白任何事情:( – Anuj

+0

您需要发送的用户名和密码到您的网络服务,并得到的回应权? – Darshana

+0

亚,但我havnt使用JSON和用户名和密码将来自文本框 – Anuj