程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> httpclient-關於HttpClient問題。為什麼安卓獲取驗證碼時,得到的是亂碼的驗證碼信息?

httpclient-關於HttpClient問題。為什麼安卓獲取驗證碼時,得到的是亂碼的驗證碼信息?

編輯:編程綜合問答
關於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調試下,應該是你沒有正確處理字符串編碼造成的。

qq_15946053
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved