這樣能正確獲取驗證碼
package com.example.day51;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private String string;
private TextView textView;
private Myhandler myhandler = new Myhandler();
private Bitmap bitmap;
private ImageView imageView;
private EditText name, psw, code;
private Button send;
private String res;
private String viewstate;
private boolean successed;
private DefaultHttpClient httpClient;
private String names;
private String passs;
private String codes;
private EditText text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview1);
imageView = (ImageView) findViewById(R.id.codeimg);
name = (EditText) findViewById(R.id.name);
psw = (EditText) findViewById(R.id.psw);
code = (EditText) findViewById(R.id.code);
send = (Button) findViewById(R.id.send);
text = (EditText) this.findViewById(R.id.text);
send.setOnClickListener(this);
new UrlThread().start();
}
private class UrlThread extends Thread {
public void run() {
super.run();
try {
new GetCodeThread().start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private class GetCodeThread extends Thread {
public void run() {
try {
getcode();
} catch (Exception e) {
e.printStackTrace();
}
super.run();
}
}
public void getcode() throws Exception {
httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://jwgl.gxufe.cn/jwweb/sys/ValidateCode.aspx");
HttpResponse httpResponse = httpClient.execute(httpPost);
byte[] bytes = EntityUtils.toByteArray(httpResponse.getEntity());
bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
myhandler.sendEmptyMessage(1);
}
private class Myhandler extends Handler {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
new GetCodeThread().start();
break;
case 1:
textView.setText(string);
imageView.setImageBitmap(bitmap);
break;
case 2:
if (!successed) {
new GetCodeThread().start();
}
break;
default:
break;
}
}
}
public void onClick(View v) {
new PostThread().start();
}
private class PostThread extends Thread {
public void run() {
super.run();
try {
myhandler.sendEmptyMessage(2);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
把網址改為:http://jw.gxufl.com/sys/ValidateCode.aspx 就不能獲取了
請求大神指點迷津
用fiddler調試下,應該是你沒有正確處理字符串編碼造成的。