各位好:
    我写了一段关于http get请求的代码,本来应该返回1的(在浏览器上直接输入这段网址可以看到返回值为1),但是在下面这段代码里面怎么返回的一直是空,请各位了解的大哥帮忙看看是怎么回事?谢谢了
代码如下:
String szrtn = "2";
String szurl   = "http://61.155.150.200:8080/mulberry/start?buddle.cuiou=mulberry&catalog.cuiou=phone.regist&name=sgjv&password=q&phone=4563";
/*建立HTTP Get联机*/
        HttpGet  httpRequest  = new HttpGet(szurl); 
        httpRequest.setHeader("Content-Type", "text/plain; charset=gb2312");
        httpRequest.setHeader("Expect", "100-continue");
        try  
        { 
            HttpParams  httpParameters  = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
            HttpConnectionParams.setSoTimeout(httpParameters, 5000); 
            
            DefaultHttpClient  httpClient  = new DefaultHttpClient(httpParameters);
         /*发送HTTP request*/
         HttpResponse  httpResponse  = httpClient.execute(httpRequest); 
         /*若状态码为200 ok*/
         if(httpResponse.getStatusLine().getStatusCode() == 200) 
         { 
         /*取得响应字符串*/
         szrtn  = EntityUtils.toString(httpResponse.getEntity());
         }
         else 
         {          } 
        } 
catch (Exception e) 
{
e.printStackTrace();
}

return szrtn;
上面代码中,返回的值szrtn永远是空是怎么回事,还忘了解的大哥们不吝赐教,谢谢了!

解决方案 »

  1.   

    上面的代码我请求http://www.baidu.com返回的是一段html的字符串,是可以正常得到返回值的,但是为什么在上面的网址里面就得不到值呢
      

  2.   

    /**
     * Get和Post提交数据
     */
    public class Main extends Activity {
    // 界面控件
    private EditText txt_nameEditText;
    private EditText txt_ageEditText;
    private Button btn_Get;
    private Button btn_Post;
    // 对象
    HttpClient httpClient = new DefaultHttpClient();
    HttpResponse httpResponse;
    HttpEntity httpEntity;
    InputStream inputStream;
    // 变量
    private static String baseUrlString = "http://10.8.8.70:88/GetAndPost.aspx";
    String nameString = "", ageString = "", resultString = ""; @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    init();
    btn_Get.setOnClickListener(new btn_Get_OnClickListener());
    btn_Post.setOnClickListener(new btn_Post_OnClickListener());
    } /**
     * 使用Get方法提交数据
     */
    private class btn_Get_OnClickListener implements OnClickListener {
    @Override
    public void onClick(View v) {
    nameString = txt_nameEditText.getText().toString().trim();
    ageString = txt_ageEditText.getText().toString().trim();
    String urlString = baseUrlString + "?name=" + nameString + "&age="
    + ageString + "&type=1";
    HttpGet httpGet = new HttpGet(urlString);
    try {
    httpResponse = httpClient.execute(httpGet);
    httpEntity = httpResponse.getEntity();
    inputStream = httpEntity.getContent();
    resultString = getString(inputStream);
    System.out.println("resultString=" + resultString);
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    inputStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }; /**
     * 使用Post方法提交数据
     */
    private class btn_Post_OnClickListener implements OnClickListener {
    @Override
    public void onClick(View v) {
    nameString = txt_nameEditText.getText().toString().trim();
    ageString = txt_ageEditText.getText().toString().trim();
    NameValuePair nvp1 = new BasicNameValuePair("name", nameString);
    NameValuePair nvp2 = new BasicNameValuePair("age", ageString);
    NameValuePair nvp3 = new BasicNameValuePair("type", "2");
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    list.add(nvp1);
    list.add(nvp2);
    list.add(nvp3);
    HttpPost httpPost = new HttpPost(baseUrlString);
    try {
    httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
    httpResponse = httpClient.execute(httpPost);
    httpEntity = httpResponse.getEntity();
    inputStream = httpEntity.getContent();
    resultString = getString(inputStream);
    System.out.println("resultString=" + resultString);
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    inputStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }; private void init() {
    txt_nameEditText = (EditText) findViewById(R.id.txt_name);
    txt_ageEditText = (EditText) findViewById(R.id.txt_age);
    btn_Get = (Button) findViewById(R.id.btn_Get);
    btn_Post = (Button) findViewById(R.id.btn_Post);
    } /**
     * 将inputStream解析成字符串返回
     */
    private String getString(InputStream stream) throws IOException {
    String str = "", lineString = "";
    BufferedReader reader = new BufferedReader(
    new InputStreamReader(stream));
    while ((lineString = reader.readLine()) != null) {
    str += lineString;
    }
    return str;
    }
    }
      

  3.   

    你自己看看吧,我写的,get和post都有。
    不过你的结贴率到0了哦,要主要结贴,不然没有人会回答你的问题的。呵呵
      

  4.   


    谢谢回复,我的get方法和你的好像没什么区别吧,我想知道的是,为什么我用我代码中的地址就不能得到get返回值呢(用别的网址是可以的),如果你知道,请一定帮忙告诉我一下,谢谢了还有就是关于结账的问题,因为我是新手,刚注册的号,我一共发过两个贴子,都是关于这个问题的,昨天才发的,因为到现在为止还没几个人回答我,所以还没有结贴。
      

  5.   

    你把我的代码里面的URL改改,试试能不能获得到东西,如果能获取到,就用我的那个吧,还简单一些。
    具体为什么,我也不是很清楚了
      

  6.   

    谢谢了,用你的代码试过了,也是得不到东西请问一下,会不会是USER_AGENT的问题呢,服务器端可不可能过滤非浏览器请求啊?
      

  7.   

    你请求的:http://61.155.150.200:8080/mulberry/start?buddle这个地址是本机的吧?
    请检查一下:1.用网页请求后查看源代码里面有东西吗?
    2.断点查看有没有抛出异常
    3.如果是本机地址不是公网地址,需要用模拟器运行,不要用真机,因为真机是无法访问到你本PC的
      

  8.   

    谢谢了哥们,我解决了
    主要原因是我没有设置
    User-agent、Accept、Accept-Language和Accept-Charse的值。
    加上这几句httpRequest.setHeader("Content-Type", "text/plain; charset=gb2312");  
                httpRequest.setHeader("User-agent", "Mozilla/5.0 (Linux; U; Android 2.2; zh-cn; Desire_A8181 Build/FRF91) AppleWebKit/533.1 (KHTML, likeGecko) Version/4.0 Mobile Safari/533.1");
                httpRequest.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
                httpRequest.setHeader("Accept-Language", "zh-cn,en-us;q=0.7,en;q=0.3"); 
                httpRequest.setHeader("Accept-Charse", "Big5,utf-8;q=0.7,*;q=0.7");  
    问题就解决了