有什么方法可以将数据从 Android Studio 写入 json?
Is there any way I can write data to json from Android Studio?
我在 https://dkapps54.github.io/jsonTestApp/message.json 举办了一个测试 json
我想制作一个带有两个 editText 的应用程序。我希望其中一个文本应替换 json 中的 "title" 字段,另一个替换 "message" 中的文本。这可能吗?
如果是,是用哪个命令做的? (我正在使用 Volley)
这是我接收消息的代码(即从 Json 请求数据):
package com.example.jsonmessagereceive;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DownloadManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
TextView title;
TextView message;
Button refresh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RequestQueue requestQueue = Volley.newRequestQueue(this);
title = findViewById(R.id.textView_title);
message = findViewById(R.id.textView_message);
refresh = findViewById(R.id.button_refresh);
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String[] titleText = {""};
final String[] messageText = {""};
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "https://dkapps54.github.io/jsonTestApp/message.json",
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
titleText[0] = response.getString("title");
messageText[0] = response.getString("message");
title.setText(titleText[0]);
message.setText(messageText[0]);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error in Response!", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error by ErrorListener!", Toast.LENGTH_LONG).show();
}
});
requestQueue.add(jsonObjectRequest);
}
});
}
}
这是您可以使用的部分代码:
private EditText editTextTitle;
private EditText editTextMessage;
JSONObject buildJsonObject() {
String title = editTextTitle.getText().toString().trim();
String message = editTextMessage.getText().toString().trim();
if (validateInput(title, message)) {
JSONObject obj = new JSONObject();
try {
obj.put("title", title);
obj.put("message", message);
return obj;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
} else {
return null;
}
}
private boolean validateInput(String title, String message) {
// check that the Strings meet whatever validation criteria you have (e.g. length, character set) and return true if all good.
// present a message (e.g. Toast) if validation failed and return false.
}
在您的 onCreate
方法中,您需要初始化 2x EditText
并在按钮上注册一个 OnClickListener
,其 onClick
方法获得一个 JSONObject
或 null
来自方法 buildJsonObject
.
我在 https://dkapps54.github.io/jsonTestApp/message.json 举办了一个测试 json 我想制作一个带有两个 editText 的应用程序。我希望其中一个文本应替换 json 中的 "title" 字段,另一个替换 "message" 中的文本。这可能吗?
如果是,是用哪个命令做的? (我正在使用 Volley)
这是我接收消息的代码(即从 Json 请求数据):
package com.example.jsonmessagereceive;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DownloadManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
TextView title;
TextView message;
Button refresh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RequestQueue requestQueue = Volley.newRequestQueue(this);
title = findViewById(R.id.textView_title);
message = findViewById(R.id.textView_message);
refresh = findViewById(R.id.button_refresh);
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String[] titleText = {""};
final String[] messageText = {""};
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "https://dkapps54.github.io/jsonTestApp/message.json",
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
titleText[0] = response.getString("title");
messageText[0] = response.getString("message");
title.setText(titleText[0]);
message.setText(messageText[0]);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error in Response!", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error by ErrorListener!", Toast.LENGTH_LONG).show();
}
});
requestQueue.add(jsonObjectRequest);
}
});
}
}
这是您可以使用的部分代码:
private EditText editTextTitle;
private EditText editTextMessage;
JSONObject buildJsonObject() {
String title = editTextTitle.getText().toString().trim();
String message = editTextMessage.getText().toString().trim();
if (validateInput(title, message)) {
JSONObject obj = new JSONObject();
try {
obj.put("title", title);
obj.put("message", message);
return obj;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
} else {
return null;
}
}
private boolean validateInput(String title, String message) {
// check that the Strings meet whatever validation criteria you have (e.g. length, character set) and return true if all good.
// present a message (e.g. Toast) if validation failed and return false.
}
在您的 onCreate
方法中,您需要初始化 2x EditText
并在按钮上注册一个 OnClickListener
,其 onClick
方法获得一个 JSONObject
或 null
来自方法 buildJsonObject
.