Java 将JSON写入/更新到URL(www.myjson.com)
我正在使用www.myjson.com作为json文件的存储,该文件将被频繁访问/更新 我目前将此作为联机json文件: 我计划经常用android studio更新它,但我不知道怎么做。如何编写/更新它,以便使用android/java代码更改json的值?Java 将JSON写入/更新到URL(www.myjson.com),java,android,json,Java,Android,Json,我正在使用www.myjson.com作为json文件的存储,该文件将被频繁访问/更新 我目前将此作为联机json文件: 我计划经常用android studio更新它,但我不知道怎么做。如何编写/更新它,以便使用android/java代码更改json的值?import android.util.Log; import android.util.Log; import java.io.BufferedInputStream; import java.io.BufferedReader; i
import android.util.Log;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class HttpHandler {
private static final String TAG = HttpHandler.class.getSimpleName();
public HttpHandler() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new GetJSON().execute();
}
private class GetJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this,"Json Data is
downloading",Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String url = "https://api.myjson.com/bins/f5fr0";
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON OBJECT node
JSONArray jsonObjInner = jsonObj.getJSONObject("channel");
String id = jsonObjInner.getString("id");
String name = jsonObjInner.getString("name");
String description = jsonObjInner.getString("description");
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
}
导入java.io.BufferedInputStream;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.ProtocolException;
导入java.net.URL;
公共类HttpHandler{
私有静态最终字符串标记=HttpHandler.class.getSimpleName();
公共HttpHandler(){
}
公共字符串makeServiceCall(字符串请求URL){
字符串响应=null;
试一试{
URL=新URL(请求URL);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“GET”);
//阅读回复
InputStream in=新的BufferedInputStream(conn.getInputStream());
响应=convertStreamToString(in);
}捕获(格式错误){
Log.e(标记“MalformedURLException:+e.getMessage());
}捕获(协议例外e){
Log.e(标记“ProtocolException:+e.getMessage());
}捕获(IOE异常){
Log.e(标记“IOException:+e.getMessage());
}捕获(例外e){
Log.e(标记,“异常:+e.getMessage());
}
返回响应;
}
私有字符串convertStreamToString(InputStream为){
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
弦线;
试一试{
而((line=reader.readLine())!=null){
sb.append(行).append('\n');
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
使某人返回字符串();
}
}
公共类MainActivity扩展了AppCompatActivity{
私有字符串标记=MainActivity.class.getSimpleName();
ArrayList联系人列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
新建GetJSON().execute();
}
私有类GetJSON扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
makeText(MainActivity.this)中的Json数据是
下载“,Toast.LENGTH_LONG).show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
HttpHandler sh=新的HttpHandler();
//向url发出请求并获得响应
字符串url=”https://api.myjson.com/bins/f5fr0";
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON对象节点
JSONArray jsonObjInner=jsonObj.getJSONObject(“通道”);
stringid=jsonObjInner.getString(“id”);
String name=jsonObjInner.getString(“名称”);
String description=jsonObjiner.getString(“description”);
}
}捕获(最终JSONException e){
Log.e(标记“Json解析错误:”+e.getMessage());
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
Json分析错误:“+e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
}否则{
e(标记“无法从服务器获取json”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
“无法从服务器获取json。请检查LogCat以了解可能的错误!”,
Toast.LENGTH_LONG).show();
}
});
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
}
}
}
您不能使用此流从应用程序更新json。要做到这一点,您需要有一个带有数据库的api。我已经读过:但我不知道如何使用它们。请使用您共享的链接中的put api。如果您知道改型,请使用volley或Reformation搜索调用put api谢谢您的努力,但我的问题是如何编写/更新json的数据,而不是从中获取数据。您需要为此创建更新api,或者创建本地数据库并将其存储是的,我已经读过这篇文章:但我很难弄清楚如何使它们工作。我还在研究这个。