如何在 android 中使用 OkHttp 从 Web api 获取 json 数据到 RecyclerView
How to get json data to RecyclerView from web api using OkHttp in android
我是 android 的新手,JSON.I 正在尝试使用 recyclerviw.I 显示来自网络服务器的 JSON 格式数据有一些客户 details.I 想要在 recyclerview.I 中显示这些详细信息知道如何在 service.But 访问网络时从 service.But 获取数据 api 我对 json 输入和 json 数据感到困惑。当我们使用 web api 时,我不明白如何提取数据。请帮我解决这个问题。
我的 Json 输入如下所示:
url : http://10.40.0.100:3009/api/GACompany/CompanyData
{
"authKey": "CD123",
"action": "1",
"idstatus": "",
"noOfRows": "0",
"id": "",
"idApplicationLogin": "",
"idGACompany": "0",
"remarks": "",
"status": "",
"noOfPointsVerified": "0"
"fileName": "",
}
我的 JSON :
{
"data": {
"comData": [
{
"applicationDate": "08-01-2019 ",
"idApplicationLogin": "AV32",
"customerName": "Anu A",
"stateName": "Kerala",
"DeadLine": "10-02-2020",
"readStatus": "Un Read"
},
]
}
public class MenuActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
dbHelper = new DbHelper(this);
}
public void listeners(View view) {
switch (view.getId()) {
case R.id.inbox_button: {
// startActivity(new Intent(this, ExampleActivity.class));
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("authKey",Const.Auth_Key_Value);
JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonObjectKeyValue);
JSONObject inboxDetailObj = new JSONObject();
inboxDetailObj.put("authKey", jsonArray);
//inboxDetailObj.put("action",4);
inboxDetailObj.put("idApplicationLogin",2012);
new PullInboxDetails(this, inboxDetailObj).execute();
} catch (JSONException e) {
e.printStackTrace();
}
}
break;
}
}
private static class PullInboxDetails extends AsyncTask<String, String, String>{
ProgressDialog pd;
WeakReference<MenuActivity> context;
OkHttpClient okHttpClient;
String url;
Request request;
MediaType JSON = MediaType.parse("application/json; charset = utf-8");
JSONObject jsonObject;
JSONObject resultJson;
String resultString;
SharedPreferences shp;
public PullInboxDetails(MenuActivity contextObj, JSONObject jsonObject) {
this.context = new WeakReference<>(contextObj);
this.jsonObject = jsonObject;
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(120,TimeUnit.SECONDS)
.build();
}
protected void onPreExecute(){
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
try {
url = "http://10.10.0.100:3009/api/GACompany/CompanyData";
RequestBody body = RequestBody.create(jsonObject.toString(), JSON);
request = new Request.Builder()
//.header("X-Client-Type", "Android")
.header("authKey","")
.url(url)
.post(body)
.build();
Response response = okHttpClient.newCall(request).execute();
if (!response.isSuccessful()) {
return "failure";
}
resultString = response.body().string();
Log.e("Log", resultString);
} catch (Exception e) {
Log.e("Log", "Exception", e);
return "failure";
}
return "success";
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// pd.dismiss(); // Error
if (s.equals("success")) {
Intent intent = new Intent(context.get(), FrontActivity.class);
intent.putExtra("value", resultString);
context.get().startActivity(intent);
} else if (s.equals("failure")) {
Toast.makeText(context.get(), "Pull Failed", Toast.LENGTH_LONG).show();
}
}
}
}
尝试像这样创建请求 JSON object
,所有 key
合而为一 jsonObject
:
JSONObject inboxDetailObj = new JSONObject();
try {
inboxDetailObj.put("authKey", "CD123");
inboxDetailObj.put("action", "1");
inboxDetailObj.put("idstatus", "");
inboxDetailObj.put("noOfRows", "0");
...
new PullInboxDetails(this, inboxDetailObj).execute();
} catch (JSONException e) {
e.printStackTrace();
}
我是 android 的新手,JSON.I 正在尝试使用 recyclerviw.I 显示来自网络服务器的 JSON 格式数据有一些客户 details.I 想要在 recyclerview.I 中显示这些详细信息知道如何在 service.But 访问网络时从 service.But 获取数据 api 我对 json 输入和 json 数据感到困惑。当我们使用 web api 时,我不明白如何提取数据。请帮我解决这个问题。
我的 Json 输入如下所示: url : http://10.40.0.100:3009/api/GACompany/CompanyData
{
"authKey": "CD123",
"action": "1",
"idstatus": "",
"noOfRows": "0",
"id": "",
"idApplicationLogin": "",
"idGACompany": "0",
"remarks": "",
"status": "",
"noOfPointsVerified": "0"
"fileName": "",
}
我的 JSON :
{
"data": {
"comData": [
{
"applicationDate": "08-01-2019 ",
"idApplicationLogin": "AV32",
"customerName": "Anu A",
"stateName": "Kerala",
"DeadLine": "10-02-2020",
"readStatus": "Un Read"
},
]
}
public class MenuActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
dbHelper = new DbHelper(this);
}
public void listeners(View view) {
switch (view.getId()) {
case R.id.inbox_button: {
// startActivity(new Intent(this, ExampleActivity.class));
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("authKey",Const.Auth_Key_Value);
JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonObjectKeyValue);
JSONObject inboxDetailObj = new JSONObject();
inboxDetailObj.put("authKey", jsonArray);
//inboxDetailObj.put("action",4);
inboxDetailObj.put("idApplicationLogin",2012);
new PullInboxDetails(this, inboxDetailObj).execute();
} catch (JSONException e) {
e.printStackTrace();
}
}
break;
}
}
private static class PullInboxDetails extends AsyncTask<String, String, String>{
ProgressDialog pd;
WeakReference<MenuActivity> context;
OkHttpClient okHttpClient;
String url;
Request request;
MediaType JSON = MediaType.parse("application/json; charset = utf-8");
JSONObject jsonObject;
JSONObject resultJson;
String resultString;
SharedPreferences shp;
public PullInboxDetails(MenuActivity contextObj, JSONObject jsonObject) {
this.context = new WeakReference<>(contextObj);
this.jsonObject = jsonObject;
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(120,TimeUnit.SECONDS)
.build();
}
protected void onPreExecute(){
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
try {
url = "http://10.10.0.100:3009/api/GACompany/CompanyData";
RequestBody body = RequestBody.create(jsonObject.toString(), JSON);
request = new Request.Builder()
//.header("X-Client-Type", "Android")
.header("authKey","")
.url(url)
.post(body)
.build();
Response response = okHttpClient.newCall(request).execute();
if (!response.isSuccessful()) {
return "failure";
}
resultString = response.body().string();
Log.e("Log", resultString);
} catch (Exception e) {
Log.e("Log", "Exception", e);
return "failure";
}
return "success";
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// pd.dismiss(); // Error
if (s.equals("success")) {
Intent intent = new Intent(context.get(), FrontActivity.class);
intent.putExtra("value", resultString);
context.get().startActivity(intent);
} else if (s.equals("failure")) {
Toast.makeText(context.get(), "Pull Failed", Toast.LENGTH_LONG).show();
}
}
}
}
尝试像这样创建请求 JSON object
,所有 key
合而为一 jsonObject
:
JSONObject inboxDetailObj = new JSONObject();
try {
inboxDetailObj.put("authKey", "CD123");
inboxDetailObj.put("action", "1");
inboxDetailObj.put("idstatus", "");
inboxDetailObj.put("noOfRows", "0");
...
new PullInboxDetails(this, inboxDetailObj).execute();
} catch (JSONException e) {
e.printStackTrace();
}