package com.gyarmy.qqlogin; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.xmlpull.v1.XmlPullParser; import com.gyarmy.qqlogin.utils.StreamTool; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { protected static final int SUCCESS = 0; protected static final int ERROR = 1; private EditText username; private EditText password; private TextView state; Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); switch (msg.what) { case SUCCESS: String data = (String)msg.obj; System.out.println(data); state.setText(data); state.setTextSize(25); break; case ERROR: Toast.makeText(MainActivity.this, "网络错误", 0); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); username = (EditText)findViewById(R.id.qq_num); password = (EditText)findViewById(R.id.qq_pass); state = (TextView)findViewById(R.id.state); } public void login(View v){ final String qq_num_str = username.getText().toString(); final String qq_pass_str = password.getText().toString(); //http://www.gyarmy.com/qqlogin.php?name=admin&pass=123456 final String path = "http://wwww.gyarmy.com/qqlogin_get.php?name="+qq_num_str+"&pass="+qq_pass_str; new Thread(){ public void run() { try { HttpClient client = new DefaultHttpClient(); HttpGet get=new HttpGet(path); HttpResponse response = client.execute(get); int code = response.getStatusLine().getStatusCode(); if(200 == code){ InputStream in = response.getEntity().getContent(); String data = StreamTool.decodeData(in); Message msg = Message.obtain(); msg.obj = data; msg.what = SUCCESS; mHandler.sendMessage(msg); }else{ Message msg = Message.obtain(); //msg.obj = data; msg.what = ERROR; mHandler.sendMessage(msg); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Message msg = Message.obtain(); //msg.obj = data; msg.what = ERROR; mHandler.sendMessage(msg); } }; }.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
0则评论给“HttpClient的get方式请求的编写”