在安卓虚拟机上运行出现 unfortunately,xxx has been stopped, Logcat中提提示的错误与HttpClient有关,有人称之为http下载数据失败。求助大家。源码:
com.example.scoreclient.loginActivitypublic class LoginActivity extends Activity {
Button login;
EditText nameView;
EditText passView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
login=(Button)findViewById(R.id.login);
nameView=(EditText)findViewById(R.id.name);
passView=(EditText)findViewById(R.id.pass);
String name=new String();
String pass=new String();
name=nameView.getText().toString();
pass=passView.getText().toString();
//检测是否登录成功,并获取登录后的Cookie
String url="http://202.115.47.141/loginAction.do";
List<Cookie> cookies = new ArrayList<Cookie>();
String resultHtml=new String();
DefaultHttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost(url);
//建立HttpPost对象
List<NameValuePair> params=new ArrayList<NameValuePair>();
//建立一个NameValuePair数组,用于存储欲传送的参数
params.add(new BasicNameValuePair("zjh",name));
params.add(new BasicNameValuePair("mm",pass));
//添加参数
try {
httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//设置编码
HttpResponse response2=null;
try {
response2 = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(response2.getStatusLine().getStatusCode()==200){//如果状态码为200,就是正常返回
//String result=EntityUtils.toString(response2.getEntity());
cookies=((AbstractHttpClient)httpclient).getCookieStore().getCookies();
//Toast.makeText(this, "登录成功", 3000).show();
//保存cookie
CookieMng cookManager=(CookieMng)getApplication();
cookManager.setCookie(cookies);
//跳转界面
Intent intent=new Intent(LoginActivity.this,ScoreActivity.class);
finish();//结束本activity
startActivity(intent);
}
else{
}
//Toast.makeText(this, "登录失败", 3000).show();
}还有CookieMng.java 和 ScoreActivity.java在此省略我的配置文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.scoreclient"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission
android:name="android.permission.INTERNET"/>
<application
android:name=".CookMng"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.scoreclient.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="com.example.scoreclient.ScoreActivity">
<intent-filter>
<action android:name="androdi.intent.action.View"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
com.example.scoreclient.loginActivitypublic class LoginActivity extends Activity {
Button login;
EditText nameView;
EditText passView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
login=(Button)findViewById(R.id.login);
nameView=(EditText)findViewById(R.id.name);
passView=(EditText)findViewById(R.id.pass);
String name=new String();
String pass=new String();
name=nameView.getText().toString();
pass=passView.getText().toString();
//检测是否登录成功,并获取登录后的Cookie
String url="http://202.115.47.141/loginAction.do";
List<Cookie> cookies = new ArrayList<Cookie>();
String resultHtml=new String();
DefaultHttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost(url);
//建立HttpPost对象
List<NameValuePair> params=new ArrayList<NameValuePair>();
//建立一个NameValuePair数组,用于存储欲传送的参数
params.add(new BasicNameValuePair("zjh",name));
params.add(new BasicNameValuePair("mm",pass));
//添加参数
try {
httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//设置编码
HttpResponse response2=null;
try {
response2 = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(response2.getStatusLine().getStatusCode()==200){//如果状态码为200,就是正常返回
//String result=EntityUtils.toString(response2.getEntity());
cookies=((AbstractHttpClient)httpclient).getCookieStore().getCookies();
//Toast.makeText(this, "登录成功", 3000).show();
//保存cookie
CookieMng cookManager=(CookieMng)getApplication();
cookManager.setCookie(cookies);
//跳转界面
Intent intent=new Intent(LoginActivity.this,ScoreActivity.class);
finish();//结束本activity
startActivity(intent);
}
else{
}
//Toast.makeText(this, "登录失败", 3000).show();
}还有CookieMng.java 和 ScoreActivity.java在此省略我的配置文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.scoreclient"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission
android:name="android.permission.INTERNET"/>
<application
android:name=".CookMng"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.scoreclient.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="com.example.scoreclient.ScoreActivity">
<intent-filter>
<action android:name="androdi.intent.action.View"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货