问题描述
- 关于HttpClient问题。为什么安卓获取验证码时,得到的是乱码的验证码信息?
-
这样能正确获取验证码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调试下,应该是你没有正确处理字符串编码造成的。
解决方案二:
用fiddler调试下,fiddler是一个抓包分析http的工具
解决方案三:
发送和接收请求的时候,设置一下unicode,就可以了
解决方案四:
有没有做编码处理啊,最好还是编码一下
解决方案五:
能教一下怎么模拟登录正方教务系统的吗
时间: 2024-10-31 05:32:27