Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.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
Java 按按钮时更新/创建JSON对象_Java_Android_Json - Fatal编程技术网

Java 按按钮时更新/创建JSON对象

Java 按按钮时更新/创建JSON对象,java,android,json,Java,Android,Json,我目前正在创建一个CMS应用程序a,我正在尝试创建一个JSON对象,我可以将其发布到我的API中,但我不知道如何做,因为我是android新手。有人有主意吗 我的代码: String URL = "http://test.soundwave.drieo.nl/api/content/" + uid + "?apikey=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"; try { APIClientJSONObject api = new

我目前正在创建一个CMS应用程序a,我正在尝试创建一个JSON对象,我可以将其发布到我的API中,但我不知道如何做,因为我是android新手。有人有主意吗

我的代码:

String URL = "http://test.soundwave.drieo.nl/api/content/" + uid + "?apikey=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";

    try {
        APIClientJSONObject api = new APIClientJSONObject();
        JSONObject result = null;

        try {
            result = api.execute(URL).get();
        } catch (Exception e) {
            e.printStackTrace();
        }

            try {

                String content = result.optString("FormattedName");
                String content2 = result.optString("Title");
                String content3 = result.optString("Subtitle");
                String content4 = result.optString("Text");

                EditText name = (EditText) findViewById(R.id.etInternNaam);
                name.setText(content);
                EditText titel = (EditText) findViewById(R.id.etName);
                titel.setText(content2);
                EditText ondertitel = (EditText) findViewById(R.id.etOndertitel);
                ondertitel.setText(content3);
                EditText EditText = (EditText) findViewById(R.id.etTekst);
                EditText.setText(Html.fromHtml(content4));

                if("null" == content) {
                    name.setText("");
                }
                if("null" == content2) {
                    titel.setText("");
                }
                if("null" == content3) {
                    ondertitel.setText("");
                }
                if("null" == content4) {
                    EditText.setText("");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
API代码:

package nl.drieo.soundwave.test.cms;

import android.os.AsyncTask;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;

/**
 * Created by r.devries on 14-3-2016.
*/
public class APIClientJSONObject extends AsyncTask<String, Void, JSONObject>           {

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

    JSONObject result = null;

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse httpResponse = httpclient.execute(new HttpGet(params[0]));
        InputStream inputStream = httpResponse.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder builder = new StringBuilder();

        String line = null;

        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        result = new JSONObject(builder.toString());
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}
包nl.drieo.soundwave.test.cms;
导入android.os.AsyncTask;
导入org.json.JSONObject;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入cz.msebera.android.httpclient.HttpResponse;
导入cz.msebera.android.httpclient.client.httpclient;
导入cz.msebera.android.httpclient.client.methods.HttpGet;
导入cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
/**
*由r.devries于2016年3月14日创建。
*/
公共类APIClientJSONObject扩展了AsyncTask{
@凌驾
受保护的JSONObject doInBackground(字符串…参数){
JSONObject结果=null;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse HttpResponse=httpclient.execute(新的HttpGet(参数[0]);
InputStream InputStream=httpResponse.getEntity().getContent();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(inputStream));
StringBuilder=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
builder.append(行);
}
结果=新的JSONObject(builder.toString());
}
捕获(例外e){
e、 printStackTrace();
}
返回结果;
}
}

您可以这样创建并将数据放入其中:

JSONObject json = new JSONObject();
json.put("user", "example");
将数据放入json后,可以通过以下方式将json传递给“APIClientJSONObject”类:
编辑:在中使用此代码为按钮单击Listener

try {
        result = api.execute(URL,json.toString()).get();
    } catch (Exception e) {
        e.printStackTrace();
    }
您可以在“APIClientJSONObject”类中获得Json,如下所示:

JSONObject jsonObject = new JSONObject(params[1])
或者,如果希望将此json“发布”到Web服务,可以使用:
编辑:在doInBackground方法中的APIClientJSONObject类中使用此命令:

HttpPost httpost = new HttpPost(params[0]);
StringEntity stringentity = new StringEntity(params[1]);

httpost.setEntity(stringentity);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
ResponseHandler responseHandler = new BasicResponseHandler();
httpclient.execute(httpost, responseHandler);

我希望这些代码对您有用。

谢谢您的回复,我现在要尝试一下,您的示例是什么意思?文本必须进入user?user的值是example请阅读更多关于JSONObjectOkay的信息谢谢,现在我已经这么做了,我的代码不识别params?这里还需要填写其他内容吗?由于某种原因,它不知道httpclient,我也不知道如何声明正确的。我还得到以下错误:StringEntity StringEntity=new StringEntity(参数[1]);未处理的异常。我必须试着抓住它吗?我也不完全理解你的意思:JSONObject JSONObject=new JSONObject(params[1]),我把它放在哪里?在我的主要活动或API中?在tyr catch中设置代码。JSONObject JSONObject=newJSONObject(params[1])只是为了理解如何将字符串强制转换为Json。对于HttpClient,您需要将此代码添加到gradle中的android方法中:useLibrary'org.apache.http.legacy'