android > android 客户端 ,PHP 服务器端 HttpGet类和HttpPost类

 

先是httpPost -------------------------------------------

PHP CODE

<?php
    header("Content-Type: text/html; charset=UTF-8");
    if(isset($_POST['username']) && isset($_POST['password']))
    {
        $username= $_POST['username'];
        $password= $_POST['password'];
        if($username== "huzhangyou"&& $password== "windows")
        {
            $array= array( 'title'=>urlencode('登陆成功'), 'id'=>1, 'value'=>'aaaaaaaaaa');
            echo urldecode(json_encode($array));
        }
        else
        {
            $array= array( 'title'=>urlencode('登陆失败'), 'id'=>1, 'value'=>md5("错误"));
            echo urldecode(json_encode($array));
        }
        break;
    }
    else
    {
        $array= array( 'title'=>urlencode('未输入'), 'id'=>1, 'value'=>md5("错误"));
        echo urldecode(json_encode($array));
    }
?>

 java Code

package socket.mft;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
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.protocol.HTTP;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class SocketActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //
        HttpPost httpPost = new HttpPost("http://192.168.1.108/android/post.php");
        HttpClient client = new DefaultHttpClient();
        StringBuilder str = new StringBuilder();
        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("username","huzhangyou"));
        params.add(new BasicNameValuePair("password","windows"));   
        BufferedReader buffer = null; 
        try
        {
            httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            HttpResponse httpRes = client.execute(httpPost);
            if(httpRes.getStatusLine().getStatusCode() == 200)
            {
                buffer = new BufferedReader(new InputStreamReader(httpRes.getEntity().getContent()));
                for(String s = buffer.readLine(); s != null; s = buffer.readLine())
                {
                    str.append(s);
                }    
                JSONObject json = new JSONObject(str.toString());              
                String title = json.getString("title");   
                int id = json.getInt("id");   
                String value = json.getString("value");    
                Log.d("log","Title:"+ title + " ID:"+ id + " Value:"+ value);     
                }
            }
        catch(Exception e)      
        {
            if(buffer != null)
            {
                try
                {
                    buffer.close();
                } catch(IOException e1) 
                {  
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            Log.d("log","服务器连接失败");
        }        
        //\\
        
        
    }
}

 

 再是http GET

 

PHP CODE

<?php
 
$array = array(
  'sendname'=>$_GET['user'],
  'username'=>'杨铸',
  'password'=>'123456',
  'user_id'=>1
);
echo json_encode($array); 

?>

 

java Code

package socket.mft;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class SocketActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //
       //http get(获取数据库包)
        startUrlCheck("user=yangtao");
        //\\
        
        
    }
    
    private void startUrlCheck(String getStr)
    {
    	HttpClient client = new DefaultHttpClient();
    	StringBuilder builder = new StringBuilder();

    	HttpGet myget = new HttpGet("http://192.168.1.108/android/get.php?"+getStr);
    	try {
    		HttpResponse response = client.execute(myget);
    		BufferedReader reader = new BufferedReader(new InputStreamReader(
    		response.getEntity().getContent()));
    		for (String s = reader.readLine(); s != null; s = reader.readLine()) {
    			builder.append(s);
    		}
    		JSONObject jsonObject = new JSONObject(builder.toString());
    		String re_sendname = jsonObject.getString("sendname");
    		String re_username = jsonObject.getString("username");
    		String re_password = jsonObject.getString("password");
    		int re_user_id = jsonObject.getInt("user_id");
    		Log.d("log", "sendname:"+re_sendname+",name:"+re_username+",pwd:"+re_password+",id:"+re_user_id);
    	} catch (Exception e) {
    		Log.d("log", "连接服务器失败");
    		e.printStackTrace();
    	}
    }    
}

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(android)