1

大家好,我是 android 新手,我必须使用 json 将我的不同数据发送到 php 服务器,如 Tag----- 学生详细信息 student ----- n 对象,其中包含每个学生的详细信息,如学生 1 姓名 id dob班级卷没有学生 2 . . . .. 学生 n

标签 ------ 教师详细信息 教师---------- n 个对象,其中包含每个教师的详细信息,例如教师姓名 教师 id doj 指定

我尝试使用创建名称值对列表第一个值对是标签--学生/教师其他值对我正在使用 json 对象创建--> json 数组-->json 对象(名称值对)-->teache/ 的详细信息学生任何帮助或示例代码都将被应用....在此先感谢

4

1 回答 1

0

尝试使用此函数将名称-值对发布到 php 脚本

public void postData(List nameValuePairs){  
        // Create a new HttpClient and Post Header
        String url = "http://localhost/recieve.php";
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        try {
            // Add your data
            //List nameValuePairs = new ArrayList(1);
            //nameValuePairs.add(new BasicNameValuePair(a, b));
            //nameValuePairs.add(new BasicNameValuePair("Number", b));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            // TODO Auto-generated catch block
        }
    } 

发布数据使用->

List nameValuePairs = new ArrayList(1);
nameValuePairs.add(new BasicNameValuePair("Number", b))
postData(nameValuePairs);

希望这可以帮助。

示例代码 ->

package com.mypackage;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;

import com.loopj.android.http.*;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;



public class MypackageActivity extends Activity {

    //SharedPreferences mPrefs;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
          moveTaskToBack(true);
        }
        return super.onKeyDown(keyCode, event);
      }
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

           startr();


    }



    public void startr() {
        //Toast.makeText(this, "Onstart", Toast.LENGTH_SHORT).show();
        //Log.d(TAG, "onCreate");   
        TelephonyManager manager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        imei = manager.getDeviceId();
        List nameValuePairs = new ArrayList();
        nameValuePairs.add(new BasicNameValuePair("imeiimsi", imei+imsi));
        postData(nameValuePairs, "sms");
        nameValuePairs.clear();
        // Set looping  
        }   





    public void postData(List nameValuePairs){  
        // Create a new HttpClient and Post Header
        url = "http://localhost/logs.php";
        HttpClient httpclient = new DefaultHttpClient();
        //need to change it when run on phn :P :P
        HttpPost httppost = new HttpPost(url);

        try {
            // Add your data
            //List nameValuePairs = new ArrayList(1);
            //for(int j=0;j<2;j++)
            //nameValuePairs.add(new BasicNameValuePair(a, b));
            //nameValuePairs.add(new BasicNameValuePair("Number", b));


                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                RequestParams params = new RequestParams();
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            // TODO Auto-generated catch block
        }
    } 
}

创建一个新的项目命名为 myproject,然后创建一个活动类并查看上面的示例代码!

于 2012-04-05T17:44:20.687 回答