如何在android中使用OkHttp从web api将json数据获取到RecyclerView



我是android和JSON的新手。我正在尝试使用recyclerview从web服务器显示JSON格式的数据。我有一些客户的详细信息。我想在回收视图中显示这些细节。我知道如何从服务中获取数据。但是当谈到web api时,我对json输入和json数据感到困惑。当我们使用web api时,我不知道如何提取数据。请帮我解决这个问题。

我的Json输入如下:网址: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();
}

最新更新