Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用json将字符串列表从android传递到php_Php_Android_Json - Fatal编程技术网

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

使用json将字符串列表从android传递到php,php,android,json,Php,Android,Json,这是我的doInBackground方法,我正在向其传递一个具有List(List>)的列表。我想直接将这个列表传递给php,而不访问它的内容。在php方面,我应该直接收到一个列表。我应该如何构建参数,以便从android发送列表 protected String doInBackground(List<ArrayList<String>>... params) { // TODO Auto-generated method stub

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

 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;
    }
受保护字符串doInBackground(列表…参数){
//TODO自动生成的方法存根
试一试{
List getli=params[0];
//建筑参数
//ArrayList params1=新的ArrayList();
Log.d(“请求!”,“启动”);
响应=CustomHttpClient.executeHttpPost(“http://10.0.2.2/menudetails.php“,参数1);
String retstring=response.toString();
Log.d(retstring,“stringgg”);
//返回字符串;
}
捕获(异常io){
msg=“无网络连接”;
}
返回null;
}
这是我用来向php发送数据和从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();

    }

   }

  }

 }

}
package com.example.androiddbconnection;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.net.URI;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.client.utils.URLEncodedUtils;
导入org.apache.http.conn.params.ConnManagerParams;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.params.HttpConnectionParams;
导入org.apache.http.params.HttpParams;
导入android.util.Log;
公共类CustomHttpClient{
静态InputStream为空;
/**客户端超时所需的时间*/
公共静态最终整数HTTP_超时=100*1000;//毫秒
/**我们的HttpClient的单个实例*/
专用静态HttpClient mHttpClient;
私有静态HttpClient getHttpClient(){
if(mHttpClient==null){
mHttpClient=新的DefaultHttpClient();
最终HttpParams params=mHttpClient.getParams();
setConnectionTimeout(参数,HTTP_超时);
HttpConnectionParams.setSoTimeout(参数,HTTP_超时);
setTimeout(参数,HTTP_超时);
}
返回mHttpClient;
}
公共静态字符串executeHttpPost(字符串url、ArrayList后参数)引发异常{
BufferedReader in=null;
试一试{
HttpClient=getHttpClient();
HttpPost请求=新的HttpPost(url);
UrlEncodedFormEntity formEntity=新的UrlEncodedFormEntity(
后参数);
请求。setEntity(formEntity);
HttpResponse response=client.execute(请求);
in=new BufferedReader(新的InputStreamReader(response.getEntity())
.getContent());
StringBuffer sb=新的StringBuffer(“”);
字符串行=”;
字符串NL=System.getProperty(“line.separator”);
而((line=in.readLine())!=null){
sb.追加(行+NL);
}
in.close();
字符串结果=sb.toString();
返回结果;
}最后{
if(in!=null){
试一试{
in.close();
}捕获(IOE异常){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
e、 printStackTrace();
}
}
}
}
公共静态字符串executeHttpGet(字符串url)引发异常{
BufferedReader in=null;
试一试{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpGet HttpGet=新的HttpGet();
setURI(新URI(url));
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
in=new BufferedReader(新的InputStreamReader(httpResponse.getEntity())
.getContent());
StringBuffer sb=新的StringBuffer(“”);
字符串行=”;
字符串NL=System.getProperty(“line.separator”);
而((line=in.readLine())!=null){
sb.追加(行+NL);
}
in.close();
字符串结果=sb.toString();
Log.d(“heyy”,结果);
返回结果;
}最后{
if(in!=null){
试一试{
in.close();
}捕获(IOE异常){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
e、 printStackTrace();
}
}
}
}
}

据我所知,您希望将ArrayList作为参数传递给PHP服务。这是不可能的,因为ArrayList是一个对象。你可以做的是先把这个列表转换成一个字符串数组(String[]),然后把它传递给你的PHP服务。我已经更改了代码