使用json将字符串列表从android传递到php

时间:2013-11-15 06:57:06

标签: php android json

这是我的doInBackground方法,我传递的List包含List(List>)。我想直接将此列表传递给php而不访问其内容。在php方面,我应该直接收到一个列表。我应该如何构建参数,以便我可以从android发送List。

 protected String doInBackground(List<ArrayList<String>>... params) {
        // TODO Auto-generated method stub

         try {

             List<ArrayList<String>> getli=params[0];
               // Building Parameters
              //ArrayList<NameValuePair> params1 = new ArrayList<NameValuePair>();


               Log.d("request!", "starting");
               response = CustomHttpClient.executeHttpPost("http://10.0.2.2/menudetails.php",params1);

            String retstring=response.toString();
            Log.d(retstring,"stringgg");

//      return retstring;
    }
         catch(Exception io){
         msg = "No Network Connection";
    }

             return null;
    }

这是我用来发送和从php获取数据的类

package com.example.androiddbconnection;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
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.client.utils.URLEncodedUtils;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.util.Log;

public class CustomHttpClient {
    static InputStream is = null;
 /** The time it takes for our client to timeout */
    public static final int HTTP_TIMEOUT = 100 * 1000; // milliseconds 
 /** Single instance of our HttpClient */
    private static HttpClient mHttpClient; 



 private static HttpClient getHttpClient() {

  if (mHttpClient == null) {
   mHttpClient = new DefaultHttpClient();
   final HttpParams params = mHttpClient.getParams();
   HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
   HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
   ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
  }


  return mHttpClient;
 }



 public static String executeHttpPost(String url,ArrayList<NameValuePair> postParameters) throws Exception {

        BufferedReader in = null;

      try {

       HttpClient client = getHttpClient();

       HttpPost request = new HttpPost(url);

       UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(

         postParameters);

       request.setEntity(formEntity);

       HttpResponse response = client.execute(request);

       in = new BufferedReader(new InputStreamReader(response.getEntity()

         .getContent()));



       StringBuffer sb = new StringBuffer("");

       String line = "";

       String NL = System.getProperty("line.separator");

       while ((line = in.readLine()) != null) {

        sb.append(line + NL);

       }

       in.close();


       String result = sb.toString();

       return result;

      } finally {

if (in != null) {

 try {

  in.close();

 } catch (IOException e) {

  Log.e("log_tag", "Error converting result "+e.toString()); 

  e.printStackTrace();

 }

}

}

}








 public static String executeHttpGet(String url) throws Exception {

  BufferedReader in = null;

  try {



      DefaultHttpClient httpClient = new DefaultHttpClient();

      HttpGet httpGet = new HttpGet();

      httpGet.setURI(new URI(url));
      HttpResponse httpResponse = httpClient.execute(httpGet);
      HttpEntity httpEntity = httpResponse.getEntity();


   in = new BufferedReader(new InputStreamReader(httpResponse.getEntity()

     .getContent()));


   StringBuffer sb = new StringBuffer("");

   String line = "";

   String NL = System.getProperty("line.separator");

   while ((line = in.readLine()) != null) {

    sb.append(line + NL);

   }

   in.close();


   String result = sb.toString();
   Log.d("heyy", result);
   return result;

  } finally {

   if (in != null) {

    try {

     in.close();

    } catch (IOException e) {

     Log.e("log_tag", "Error converting result "+e.toString()); 

     e.printStackTrace();

    }

   }

  }

 }

}

0 个答案:

没有答案