2012-10-30 93 views
5
  try 
     { 

      url= new URL(ConstantsClass.VENDOR_FOLLOW + "?UID=" +android_id+"&URL='"+resultfinal+"'&device=android"); 


       connection = (HttpURLConnection) url.openConnection(); 
       connection.setDoOutput(true); 
       connection.setRequestMethod("POST"); 

       request = new OutputStreamWriter(connection.getOutputStream()); 
       request.flush(); 
       request.close(); 
       request.write("Hello!!!"); 

       String line = ""; 
       InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
       BufferedReader reader = new BufferedReader(isr); 

       StringBuffer sb = new StringBuffer(); 

       while((line=reader.readLine())!=null) { 
        sb.append(line + "&"); 
       } 

       response = sb.toString(); 
       //response.getEntity().getContent(); 

       Log.i("Test", "updated response: " + response); 



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

      Log.i("Test", "**************url list********************" + url); 
     tag_text.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        Intent in=new Intent(context,LinkWebView.class); 
        in.putExtra("vendorUrl", resultfinal); 
        context.startActivity(in);  
        //postData(); 
       } 
      }); 
      } 

    tag_text.setTextSize(16); 
    return view; 

} 

嗨,我是新來的android和我試圖從URL傳遞值到服務器,但我得到空值傳遞到服務器端。更新響應爲空。我的服務器端值不給我任何價值。我需要從上面給出的url傳遞url,android_id和device。我也嘗試了httpclient,但它也給了我null值。http post方法將空值傳遞給服務器

+1

檢查您是否在傳遞到URL之前正確地獲得了所有值.BTW「resultfinal」包含哪些內容? – AndroidLearner

+0

resultfinal有一個我正在傳遞的網址。而且我已經在烤麪包上打印了這個url值,它正確地給出了所有的值 –

+0

android_id是一個數字還是一個字符串? – Bhavin

回答

11

你應該嘗試下面的代碼,它對我的​​運行非常好。

// ADD YOUR REQUEST DATA HERE (you can pass number of variable). 
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("Your_var_1", value)); 
    nameValuePairs.add(new BasicNameValuePair("Your_var_2", value)); 

現在建立像

(1)發送簡單的字符串到服務器

try 
    { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("your url only ex:www.google.com/abc"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpParams httpParameters = new BasicHttpParams(); 
     DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
    } catch (Exception e) 
    { 
     Log.e("Loading Runnable Error in http connection :", e.toString()); 
    } 

(2)發送JSON編碼字符串服務器

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

try { 
HttpPost post = new HttpPost(URL); 
json.put("user_name", "chintan"); 
json.put("password", "khetiya"); 
StringEntity se = new StringEntity(json.toString()); 
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
post.setEntity(se); 
response = client.execute(post); 

/*Checking response */ 
if(response!=null){ 
is = response.getEntity().getContent(); //Get the data in the entity 
} 

} catch(Exception e) { 
e.printStackTrace(); 
createDialog("Error", "Cannot Estabilish Connection"); 
} 

響應您的網絡連接將在兩種情況下都相同

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(); 
     result = sb.toString(); 
    } 
    catch (Exception e) 
    { 
     Log.e("Loading Runnable Error converting result :", e.toString()); 
    } 

現在最終結果包含整個輸出字符串,現在它取決於您將如何讀取數據。使用json或其他。我正在使用json,所以把它的示例代碼可能會對你有所幫助。

JSONObject json_data = new JSONObject(result);// its a string var which contain output. 
     my_output_one = json_data.getString("var_1"); // its your response var form web. 
     my_output_two = json_data.getString("var_2"); 

現在它的上面有兩個變量,它們具有任何類型的值和使用任何類型的變量。

現在這對你有幫助。如果您有任何疑問,請告訴我。

+0

嗨辛登我已經嘗試過這樣做,它仍然只給出空值 –

+0

當我把吐司打印網址我給了我正確的價值觀所有的價值觀是正確的。 –

+0

php我有它的日誌文件,但它給了我在服務器端的空值 –

1

請在寫入後調用flush並在finally塊中關閉流。看看下面的代碼:

try 
     { 

      url= new URL(ConstantsClass.VENDOR_FOLLOW + "?UID=" +android_id+"&URL='"+resultfinal+"'&device=android"); 


       connection = (HttpURLConnection) url.openConnection(); 
       connection.setDoOutput(true); 
       connection.setRequestMethod("POST"); 

       request = new OutputStreamWriter(connection.getOutputStream()); 
       request.write("Hello!!!"); 

       request.flush(); 


       String line = ""; 
       InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
       BufferedReader reader = new BufferedReader(isr); 

       StringBuffer sb = new StringBuffer(); 

       while((line=reader.readLine())!=null) { 
        sb.append(line + "&"); 
       } 

       response = sb.toString(); 
       //response.getEntity().getContent(); 

       Log.i("Test", "updated response: " + response); 



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

      Log.i("Test", "**************url list********************" + url); 
     tag_text.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        Intent in=new Intent(context,LinkWebView.class); 
        in.putExtra("vendorUrl", resultfinal); 
        context.startActivity(in);  
        //postData(); 
       } 
      }); 
      } 

    tag_text.setTextSize(16); 
    return view; 

}finally{ 
       try{ 
       request.close(); 
       }catch(Exception e){} 
} 
+0

不,它仍然只給出空值 –

+0

請嘗試一些休息工具來檢查你發送的請求是否正確,並且服務器正在發送一些響應。你可以使用下面的休息插件來測試:https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo –

相關問題