将arraylist bean从android传递到webservice php [英] pass arraylist bean from android to webservice php

查看:25
本文介绍了将arraylist bean从android传递到webservice php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 android 新手,使用网络服务

i'm newbie in android with web service

我正在尝试将 arraylist 从 android 传递到 webservice php 服务器

i'm trying to pass arraylist from android to webservice php server

这是我的 bean 代码:

here's my bean code:

public class ExpressionBean {
    public static final String EXPRESSION_ID = "expressionID";
    public static final String EXPRESSION_TEXT = "expressionText";
    public static final String ANS_TEXT1 = "ansText1";
    public static final String ANS_TEXT2 = "ansText2";
    public static final String ASSESSEE_ANSWER = "assesseeAnswer";

    private String expressionID;
    private String expressionText;
    private String ansText1;
    private String ansText2;
    private String assesseeAnswer;

    public String getExpressionID() {
        return expressionID;
    }

    public void setExpressionID(String expressionID) {
        this.expressionID = expressionID;
    }

    public String getExpressionText() {
        return expressionText;
    }

    public void setExpressionText(String expressionText) {
        this.expressionText = expressionText;
    }

    public String getAnsText1() {
        return ansText1;
    }

    public void setAnsText1(String ansText1) {
        this.ansText1 = ansText1;
    }

    public String getAnsText2() {
        return ansText2;
    }

    public void setAnsText2(String ansText2) {
        this.ansText2 = ansText2;
    }

    public String getAssesseeAnswer() {
        return assesseeAnswer;
    }

    public void setAssesseeAnswer(String assesseeAnswer) {
        this.assesseeAnswer = assesseeAnswer;
    }

}

这是我关于异步任务的 doInBackround :

and here's my doInBackround on async task :

protected Boolean doInBackground(Void... params) {
            // TODO: attempt authentication against a network service.

            boolean result = false;
            // test = new TestBean();

            // int resultTest = 0;
            UserFunctions userFunction = new UserFunctions();

            Log.d(TAG, "UID : " + mEmail);
            // Log.d(TAG, "resultTest : " + resultTest);
            JSONObject jsonTest = userFunction.storeTest(mEmail);
            Log.d(TAG, "After JSON TEST ");

            try {
                if (jsonTest.getString(KEY_SUCCESS) != null) {
                    String res = jsonTest.getString(KEY_SUCCESS);
                    JSONObject testData = jsonTest.getJSONObject(TAG_TEST);
                    test = new TestBean();
                    test.setTestid(testData.getInt(TAG_TEST_ID));
                    test.setUid(testData.getInt(TAG_UID));
                    JSONArray list = new JSONArray();
                    String list2;
                    for (int position = 0; position < expressionList.size(); position++) {
                        Gson gson = new Gson();
                        list.put(gson.toJson(expressionList.get(position)));

                    }
                    Log.d(TAG, "JSONArray list coy : " + list);
                    UserFunctions uf = new UserFunctions();

                    JSONObject jsonHistoryList = new JSONObject();
                    jsonHistoryList = uf.storeHistoryList(list.toString());
                    if (Integer.parseInt(res) == 1) {
                        result = true;
                        finish();
                    } else {
                        result = false;
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                return false;
            }

            // TODO: register the new account here.
            return result;
        }

这里是 storeHistoryList 方法:

and here's storeHistoryList Method :

public JSONObject storeHistoryList(String list) {

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", storeHistory_tag));
        params.add(new BasicNameValuePair("list", list));

        JSONObject json = jsonParser.getJSONFromUrl(URL, params);
        return json;
    }

我想将列表传递给网络服务

i want to pass list to web service

list 是一个数组列表 ExpressionBean

list is an arraylist ExpressionBean

我使用 gson 将 bean 转换为 json

i used gson for convert bean to json

但是当我执行时,日志说

but when i execute, the log said

"解析数据时出错...

"error parsing data...

jsonarray 无法转换为 jsonobject

jsonarray cannot be converted to jsonobject

我必须做什么?谢谢

推荐答案

试试这个代码

JSONParser.java


  public class JSONParser
    {

        static InputStream is ;
        static JSONObject jObj = null;
        static String json = "";

        String Dataurl = "";
        // constructor
        public JSONParser(String url) 
        {
            Dataurl = url;
        }

        // function get json from url by making HTTP POST or GET method
        public JSONObject makeHttpRequestResponse(String method,List<NameValuePair> Data_Request_Response) 
        {
            try {

                // check for request method
                if(method == "POST_Request_Response")
                {

                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(Dataurl);
                    httpPost.setEntity(new UrlEncodedFormEntity(Data_Request_Response));

                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();

                }
                else if(method == "GET_Request_Response")
                {
                    HttpClient httpClient = new DefaultHttpClient();
                    String paramString = URLEncodedUtils.format(Data_Request_Response, "utf-8");
                    Dataurl += "?" + paramString;
                    HttpGet httpGet = new HttpGet(Dataurl);

                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
                }


            } 
            catch (UnsupportedEncodingException e) 
            {
                e.printStackTrace();
            } 
            catch (ClientProtocolException e)
            {
                e.printStackTrace();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            try 
            {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) 
                {
                    sb.append(line + "
");
                }
                is.close();
                json = sb.toString();
            } 
            catch (Exception e) 
            {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }

            // try parse the string to a JSON object
            try 
            {
                jObj = new JSONObject(json);
            } 
            catch (JSONException e) 
            {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }

            // return JSON String
            return jObj;    
        }// End Http Request Response   
    }

Yourfilename.java



 // Object of the Json Parser Class
    JSONParser mJsonParser = new JSONParser(DataUrl);
    JSONObject mJsonObject_Request = new JSONObject();

        List<NameValuePair> Send_Request = new ArrayList<NameValuePair>();
        Send_Request.add(new BasicNameValuePair("Token", "Data"));
        Send_Request.add(new BasicNameValuePair("Token1","Data"));



            try {
                mJsonObject_Request = mJsonParser.makeHttpRequestResponse("POST_Request_Response",Request);

                Log.d("No Of Tables", "" + mJsonObject_Request.names().length());
                Log.d("Name Of Tables", "" + mJsonObject_Request.names());
                Log.d("DATA", "" + mJsonObject_Request);

           } 
           catch (Exception e) {


          }

此处的数据 URL 是您的网络服务链接.

here data URL is your web service link.

这篇关于将arraylist bean从android传递到webservice php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆