要获取JSON响应,可以使用Android提供的Volley库或者使用Java的HttpURLConnection类。以下是两种解决方法的示例代码:
使用Volley库:
dependencies {
implementation 'com.android.volley:volley:1.2.0'
}
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;
public class MainActivity extends AppCompatActivity {
private RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化请求队列
requestQueue = Volley.newRequestQueue(this);
// 发起获取JSON响应的请求
getJsonResponse();
}
private void getJsonResponse() {
String url = "https://api.example.com/data.json";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
// 处理JSON响应
try {
// 从JSON对象中获取数据
String data = response.getString("data");
// 处理数据...
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// 处理错误响应
error.printStackTrace();
}
});
// 将请求添加到请求队列中
requestQueue.add(request);
}
}
使用HttpURLConnection类:
import android.os.AsyncTask;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 发起获取JSON响应的请求
new GetJsonResponseTask().execute();
}
private class GetJsonResponseTask extends AsyncTask {
@Override
protected JSONObject doInBackground(Void... voids) {
String url = "https://api.example.com/data.json";
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
// 创建URL对象
URL apiUrl = new URL(url);
// 打开连接
connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestMethod("GET");
// 获取输入流
InputStream inputStream = connection.getInputStream();
// 创建BufferedReader对象来读取输入流
reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
// 读取输入流并将其存储在StringBuilder中
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
// 将JSON字符串转换为JSONObject
return new JSONObject(stringBuilder.toString());
} catch (IOException | JSONException e) {
e.printStackTrace();
} finally {
// 关闭连接和BufferedReader
if (connection != null) {
connection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(JSONObject response) {
if (response != null) {
// 处理JSON响应
try {
// 从JSON对象中获取数据
String data = response.getString("data");
// 处理数据...
} catch (JSONException e) {
e.printStackTrace();
}
} else {
// 处理错误响应
}
}
}
}
这些代码示例可以用来获取JSON响应,并在获取到响应后进行相应的处理。