2 qq 15946053 qq_15946053 于 2015.07.21 23:28 提问

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

请求大神指点迷津

5个回答

devmiao
devmiao   Ds   Rxr 2015.07.21 23:40
已采纳

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

qq_15946053
qq_15946053 谢谢你
接近 2 年之前 回复
qq_15946053
qq_15946053 不明白?这是
2 年多之前 回复
caozhy
caozhy   Ds   Rxr 2015.07.22 06:13

用fiddler调试下,fiddler是一个抓包分析http的工具

qq_15946053
qq_15946053 项目做完了,非常感谢你的回答
接近 2 年之前 回复
qq_15946053
qq_15946053 这个怎么用在 不是用HttpClient吗
2 年多之前 回复
weixin_29444181
weixin_29444181   2015.07.22 07:59

发送和接收请求的时候,设置一下unicode,就可以了

yuke198907
yuke198907   2015.07.22 09:35

有没有做编码处理啊,最好还是编码一下

yuke198907
yuke198907 回复qq_15946053: 你怎么处理的,代码看看
2 年多之前 回复
qq_15946053
qq_15946053 做了编码处理也没有成功的
2 年多之前 回复
u014743199
u014743199   2015.11.27 12:19

能教一下怎么模拟登录正方教务系统的吗

Csdn user default icon
上传中...
上传图片
插入图片