0 0

android httpclient 连不上5

目的:用户输入账号和密码后,根据客户端连接服务器,登陆成功为1,不成功为0。

//根据具体的url连接获得数据
public static String queryStringForPost(String url){
HttpPost request = HttpUtil.getHttpPost(url);
String result = null;
try {
HttpResponse response = HttpUtil.getHttpResponse(request);//这行发生了异常
                    if(response.getStatusLine().getStatusCode()==200){
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "网络异常!";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "网络异常!";
return result;
}
        return null;
    }

//异常的方法
public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException, IOException{
HttpResponse response = new DefaultHttpClient().execute(request);
return response;
}


问题补充:由于传值的原因,所以我将post也定义成get了,参数在url中直接带过来了,所以这就不存在get和post区别

问题补充:
sanfeng_chow 写道
你先检查一下虚拟机是否可以上网。
检查方法用虚拟机的浏览器打开一个网站。




大哥,的确打不开网站,但是小弟不清楚哪里设置了不能上网哇,希望指点下,以下是我的截图

问题补充:
renpeng301 写道
Androidmanifest.xml你加这个权限么?

<uses-permission android:name="android.permission.INTERNET" />
</manifest>


大哥我用你的方法果然可以上网了,但是网上很多写的要求设置代理IP和设置DNS这是做什么的啊?

问题补充:renpeng301 写道
Androidmanifest.xml你加这个权限么?

<uses-permission android:name="android.permission.INTERNET" />
</manifest>


大哥我用你的方法果然可以上网了,但是网上很多写的要求设置代理IP和设置DNS这是做什么的啊?

问题补充:
renpeng301 写道
Androidmanifest.xml你加这个权限么?

<uses-permission android:name="android.permission.INTERNET" />
</manifest>



大哥,我加了这权限后,访问网站是解决了,但是,代码到
HttpResponse response = HttpUtil.getHttpResponse(request);还是发生了IOException,

问题补充:renpeng301 写道
Androidmanifest.xml你加这个权限么?

<uses-permission android:name="android.permission.INTERNET" />
</manifest>



大哥,我加了这权限后,访问网站是解决了,但是,代码到
HttpResponse response = HttpUtil.getHttpResponse(request);还是发生了IOException,

问题补充:这是我的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="huigou.com"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".LoginActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name="MainActivity"/>
<activity android:name="huigou.com.RegeditActivity"/>
<activity android:name="huigou.com.ResultActivity"/>
<activity android:name="huigou.com.BehaviorActivity"/>
<activity android:name="huigou.com.MenuActivity"/>
<activity android:name="huigou.com.PromotionActivity"/>
<activity android:name="huigou.com.ShopActivity"/>
<activity android:name="huigou.com.TabMActivity"/>
<activity android:name="huigou.com.TeamActivity"/>

    </application>
   
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
    <uses-sdk android:minSdkVersion="3" />

</manifest>

问题补充:
renpeng301 写道
你使用 android模拟器 连接 你的本地项目
“http://localhost:8080/Test/"
这么?



是的,我通过get,将数据传递到服务端,然后再服务端做判断得出0或者1,在页面上,具体的url=http://localhost:8080/Test/c/user.do?method=login
但是具体的错误就放生在访问服务器的response响应上,我用IE打开url是可以获得正确值的
但是用HttpResponse response = HttpUtil.getHttpResponse(request);来获得response时候就发生了错误

问题补充:上面url写错了应该是:
url=http://localhost:8080/Test/c/user.do?method=login
&username=123&password=123
2011年1月04日 10:51
  • 大小: 45.2 KB

11个答案 按时间排序 按投票排序

0 0

采纳的答案

访问本机ip设置为10.0.2.2,不要使用localhost

2011年1月05日 11:11
0 0

你的action返回的什么?
  你贴下你的action

2011年1月04日 20:30
0 0

你使用 android模拟器 连接 你的本地项目
“http://localhost:8080/Test/"
这么?

2011年1月04日 16:54
0 0

你打印下异常信息? 要不你上传你的工程·我下班给你看看 ··

2011年1月04日 16:52
0 0

这种可能是 模拟器的默认DNS与电脑的DNS不一致·
这样就得设置DNS
   但是我没碰到过着情况啊·· 

2011年1月04日 16:01
0 0

只要你的机器可以上网··模拟机就可以 我用了这么久 没遇到过设置什么代理啊

2011年1月04日 15:54
0 0

Androidmanifest.xml你加这个权限么?

<uses-permission android:name="android.permission.INTERNET" />
</manifest>

2011年1月04日 15:46
0 0

设置权限么?

2011年1月04日 15:42
0 0

既然都是Get方法的话,但在
private String query(String username,String password){
  String queryString = "username="+username+"&password="+password;
  String url =    HttpUtil.BASE_URL+"m/user.domethod=login&"+queryString;
  return HttpUtil.queryStringForPost(url);
}却是queryStringForPost(url)?是否换成queryStringForGet(url)...

2011年1月04日 14:54
0 0

你先检查一下虚拟机是否可以上网。
检查方法用虚拟机的浏览器打开一个网站。

2011年1月04日 14:46
0 0

似乎是post和get有点混乱。。。。
建议看看
http://aina-hk55hk.iteye.com/blog/711544

2011年1月04日 13:57

相关推荐

Global site tag (gtag.js) - Google Analytics