如何使用ArrayList创建此JSON以在Android中发布json
{
"user" :
{
"nickname" : "nickname6",
"password" : "1234",
}
}
我只得到一个扁平的JSON
ArrayList<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
nameValuePairs.add(new BasicNameValuePair("nickname","nickname6"));
nameValuePairs.add(new BasicNameValuePair("password", "1234"));
答案 0 :(得分:2)
您需要创建一个JSON,为此您必须在POST方法中添加该JSON作为参数。要做到这一点,你可以试试这个:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php\?/ [NC]
RewriteRule (.*?)index\.php\?/ / [R=301,NE,L]
答案 1 :(得分:2)
如您所知,HttpClasses,NameValuePair和BasicNameValuePair已在最新的android中弃用。我们现在应该避免它。
如果你想创建
{
"user":{
"nickname":"nickname6"
"password":"1234",
}
}
您可以使用下面的代码示例使用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();
}
答案 2 :(得分:0)
尝试像这样使用ContentValues
ContentValues values=new ContentValues();
values.put("username",name);
values.put("password",password);
OR 使用MultipartEntity
MultipartEntity multi = new MultipartEntity();
multi.addPart("name", new StringBody("your data"));
multi.addPart("Id", new StringBody("123"));
答案 3 :(得分:0)
这里是使用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();
}
}
}
然后你可以从onCreate或OnClickListener中调用它来获取一个按钮,如下所示:
new httpSendrequest().execute();
希望这会有所帮助:)