xiaowuhls 2015-07-21 15:28 采纳率: 100%
浏览 1892
已采纳

关于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 就不能获取了

请求大神指点迷津

  • 写回答

4条回答

  • devmiao 2015-07-21 15:40
    关注

    用fiddler调试下,应该是你没有正确处理字符串编码造成的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?