先是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();    }    }    }

更多相关文章

  1. This Android(安卓)SDK requires Android(安卓)Developer To...
  2. Android(安卓)Ble
  3. adb connect 失败时(unable to connect to)解决办法
  4. 编译V8静态库 for Android
  5. Apktool 回编译出现No resource identifier产生compileSdkVersio
  6. android HttpURLConnection 连接网络 读取返回数据
  7. Android(安卓)- MediaPlayer
  8. Android通过http协议POST传输方式
  9. Android网络连接判断与处理

随机推荐

  1. Android(安卓)adb.exe程序启动不起来,如
  2. 【Android】IPC、AIDL、Binder
  3. Android之用自定义的shape去实现shadow效
  4. APP安装支持安装到SDCard
  5. Android与设计模式(4)外观/桥接/MVP
  6. Android(安卓)- Layout时发生'Unfortunat
  7. Android笔记 从init到SystemServer
  8. Android中MaterialDesign使用 (五)自定义
  9. Android的消息机制
  10. android防止内存溢出浅析