网络权限已设置。隔几分钟出来一次request time failed:java.net.socketException:Address family not surpported by protocol。主要代码贴出来:
String kinds=kindsText.getText().toString();
String area=areaText.getText().toString();
NameValuePair kindsValuePair=new BasicNameValuePair("kinds",kinds);
NameValuePair areaValuePair=new BasicNameValuePair("area",area);
List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
nameValuePairs.add(kindsValuePair);
nameValuePairs.add(areaValuePair);
InputStream inputStream=null;
String baseUrl="http://120.95.137.237:8080/Test/MyJsp.jsp";
HttpPost httpPost=new HttpPost(baseUrl);
try{
HttpEntity requestHttpEntity=new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8);
httpPost.setEntity(requestHttpEntity);
HttpClient httpClient=new DefaultHttpClient();
httpResponse=httpClient.execute(httpPost);
httpEntity=httpResponse.getEntity();
inputStream=httpEntity.getContent();
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
String result="";
String line="";
while((line=br.readLine())!=null){
result=result+line;
}
System.out.print(result);
}catch(Exception e){
e.printStackTrace();
}
String kinds=kindsText.getText().toString();
String area=areaText.getText().toString();
NameValuePair kindsValuePair=new BasicNameValuePair("kinds",kinds);
NameValuePair areaValuePair=new BasicNameValuePair("area",area);
List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
nameValuePairs.add(kindsValuePair);
nameValuePairs.add(areaValuePair);
InputStream inputStream=null;
String baseUrl="http://120.95.137.237:8080/Test/MyJsp.jsp";
HttpPost httpPost=new HttpPost(baseUrl);
try{
HttpEntity requestHttpEntity=new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8);
httpPost.setEntity(requestHttpEntity);
HttpClient httpClient=new DefaultHttpClient();
httpResponse=httpClient.execute(httpPost);
httpEntity=httpResponse.getEntity();
inputStream=httpEntity.getContent();
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
String result="";
String line="";
while((line=br.readLine())!=null){
result=result+line;
}
System.out.print(result);
}catch(Exception e){
e.printStackTrace();
}
解决方案 »
- android客户端如何将存在arraylist中的多条数据,通过json上传到服务器端
- 如何让列表右边能带一个CheckBox?
- android 横屏后如何确保数据不丢失。
- EidtText固定宽度后,如何不换行,加内容,内容自动向左挪?
- 布局问题
- 关于epoll的一些问题
- 【android】这个动画效果如何实现呢?
- 请问关于提示 newInstance()没有定义的问题
- 请问有人知道 怎么把 Android-PullToRefresh 整合到IDEA里面吗?
- Android studio 使用git版本控制,怎么使用账号和密码不使用ssh免密登录?
- [Share]最近研究Android command,稍微总结一下分享出来
- 考勤系统
* Role:发送会议ID和Name去服务器验证并取得返回值
* <BR>Date:2012-2-10
* <BR>@author CODYY)peijiangping
*/
public String intermessage(String str1, String str2) {
//请求路径
String url = context.getString(R.string.clienturl)+"/TestServlet";
HttpConnect httpConnect = new HttpConnect(url, context);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("meetid", str1));
params.add(new BasicNameValuePair("name", str2));
params.add(new BasicNameValuePair("username", LoginActivity.username));
String result = httpConnect.getDataAsString(params);
return result;
}
然后
private String url;
private HttpPost httpRequest;
private HttpResponse httpResponse;
private HttpParams httpParams;
private HttpClient httpClient;
//private static final String YES = "yes";
private static final String NO = "no"; public HttpConnect(String url, Context context) {
this.url = url;
httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 15000);
HttpConnectionParams.setSoTimeout(httpParams, 15000);
httpClient = new DefaultHttpClient(httpParams);
}/**
* Role:通过Http请求来获取返回值(String)
* <BR>Date:2012-2-10
* <BR>@author CODYY)peijiangping
*/
public String getDataAsString(List<NameValuePair> params) {
String result = null;
try {
httpRequest = new HttpPost(url);
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
httpResponse = httpClient.execute(httpRequest);
//System.out.println("httpResponse"+httpResponse.getStatusLine().getStatusCode()+"url:"+url);
if (200 == httpResponse.getStatusLine().getStatusCode()) {
result = EntityUtils.toString(httpResponse.getEntity());
System.out.println("取得返回值" + result);
if (result.equals(NO)) {
return NO;
} else {
return result;
}
}
} catch (IOException e) {
return "error";
}
return NO;
}
首先确定已经加上了<uses-permission android:name="android.permission.INTERNET" />
权限
如果加上了,
你可以用真机试试,
我之前用模拟器也出现过类似的问题,但用真机没问题,,或者你新建另一个模拟器,用新的模拟器看看,你的代码,和楼上提供的代码是没有问题的
今天换成无线了,peijiangping1989的代码没有问题了。但是我的代码一会能打印服务器返回的参数,一会又不行。不知道这是什么情况。