当前位置: 首页 > 面试题库 >

将arraylist bean从android传递到webservice php

漆雕和昶
2023-03-14
问题内容

我是具有网络服务的android新手

我试图将arraylist从android传递到webservice php服务器

这是我的bean代码:

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:

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方法:

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;
    }

我想将列表传递给Web服务

list是一个arraylist ExpressionBean

我用gson将bean转换为json

但是当我执行时,日志说

“错误解析数据…

jsonarray无法转换为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 + "\n");
                }
                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是您的Web服务链接。



 类似资料:
  • 问题内容: 我有一个包含项目类别的列表视图。当我在列表上按类别标题时,它将显示另一个包含所选类别内项目的列表视图。 我通过使用ListFragment来做到这一点。我是否必须启动一个新活动并按意图一起传递类别ID? 这是我的类别: 我要添加什么以转到“项目列表”?我需要使用另一个ListFragment还是仅使用常规ListActivity? 如何从解析的JSON中检索类别ID并将其传递给列表项?

  • 我正在尝试从我的FoodAdapter中传递数据,该数据从BaseAdapter扩展到从AppCompatDialog片段扩展的FoodDialog。当用户单击list_item中的按钮时,我需要将数据传递给FoodDialog。 确认FoodDialog。班

  • 我正在尝试将包含object的ArrayList从servlet传递到JSP。但是 Servlet文件: JSP文件:

  • 我正在尝试将某些值从servlet传递到JSP页面,并添加已传递到标记的值,阅读了许多文章,我得到了以下代码。 使用输入页面选择文件 验证上传的文件 调用上传。java将上传的文件保存在WEB-INF中 在上载的文件中,选定的文件保存为“我的”。txt 使用缓冲区读取文件内容并将其保存到变量 将其传递到JSP页面 上载JAVA 上传文件后, mypage.jsp 现在,当我点击上传按钮完成所有这些

  • 问题内容: 当我尝试将Servlet中的列表值设置为会话变量并像JSP一样访问它时, 做得到 JSP 我在JSP中遇到错误,因为“列表无法解析为类型” 那我该怎么做呢?我想将列表从Servlet传递到JSP并填充一个下拉列表。 问题答案: 您要导入列表和演员表吗?

  • 问题内容: 我想将值传递给javascript。如果可能的话,我该怎么办?如何在后备bean中接收它们? 问题答案: