简体   繁体   English

使用BasicNameValuePair创建复杂的JSON

[英]creating complex JSON using BasicNameValuePair

How can I create this JSON using ArrayList for posting json in Android 如何使用ArrayList创建此JSON以在Android中发布JSON

{
"user" : 
    {
        "nickname" : "nickname6",
        "password" : "1234",
    }
}

I get only to a flat JSON 我只得到一个平面JSON

    ArrayList<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();

    nameValuePairs.add(new BasicNameValuePair("nickname","nickname6"));
    nameValuePairs.add(new BasicNameValuePair("password", "1234"));

You need to create a JSON, and for that you have to add that JSON as a parameter in your POST method. 您需要创建一个JSON,为此您必须将该JSON作为参数添加到POST方法中。 To do that you can try this : 为此,您可以尝试以下操作:

JSONObject json = new JSONObject(jsonString);
JSONObject joParams = json.getJSONObject("params");
String nickname = joParams.getString("nickname");

    post.add(new BasicNameValuePair("nickname", nickname);

As you know, HttpClasses, NameValuePair and BasicNameValuePair have been deprecated in latest android. 如您所知,HttpClasses,NameValuePair和BasicNameValuePair在最新的android中已被弃用。 we should avoid it now. 我们现在应该避免它。

and If you want to create 如果要创建

{
   "user":{
        "nickname":"nickname6"
        "password":"1234",             
    }
}

Than you can use below code sample to create the same json using JSONObject class. 比起下面的代码示例,您可以使用JSONObject类创建相同的json。

JSONObject jObj = new JSONObject();

try {       
    JSONObject userCredentials = new JSONObject();
    userCredentials.put("nickname","nickname6");
    userCredentials.put("password","1234");

    jObj.put("user", userCredentials);
} catch(Exception e) {
   e.printStackTrace();
}

try to use ContentValues like this way 尝试像这样使用ContentValues

ContentValues values=new ContentValues();
values.put("username",name);
values.put("password",password);

OR Use MultipartEntity 或使用MultipartEntity

MultipartEntity multi = new MultipartEntity();
multi.addPart("name", new StringBody("your data"));
multi.addPart("Id", new StringBody("123"));

here's an example for http post using AsyncTask: 这是使用AsyncTask的http帖子示例:

public class httpSendrequest extends AsyncTask<Void,Void,String> {

        ArrayList<NameValuePair> nvPairs=new ArrayList<>();

    Boolean error=false;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            //here you initialize your json object and add it to value pairs
            JSONObject jObj = new JSONObject();

    try {       
            JSONObject userCredentials = new JSONObject();
            userCredentials.put("nickname","nickname6");
            userCredentials.put("password","1234");

            jObj.put("user", userCredentials);
    nvPairs.add(new BasicNameValuePair("jsonstring",jObj.toString()));
    } catch(Exception e) {
      error=true;
    }

        }

        @Override
        protected String doInBackground(Void... params) {

    if(!error)
            try{

                String link ="http://"+ip+"/QSystem.asmx/insert_answer" ;
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(link); //adding URL to the http post
                httppost.setEntity(new UrlEncodedFormEntity(nvPairs, HTTP.UTF_8)); //adding the value pairs and encoding to the http post request
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity resEntity = response.getEntity();


            } catch (Exception e){
                error=true;
            }

            return "str";
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            if(!error) {
                Toast.makeText(getApplicationContext(), "Sent", Toast.LENGTH_SHORT).show();

            }
        }

    }

then you can call it from onCreate or in an OnClickListener for a button, like this: 那么您可以从onCreate或在OnClickListener中为按钮调用它,如下所示:

new httpSendrequest().execute();

hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM