【发布时间】:2020-11-04 11:04:41
【问题描述】:
我是 android 的新手,也是 JSON。我正在尝试使用 recyclerviw 从 Web 服务器显示 JSON 格式的数据。我有一些客户详细信息。我想在 recyclerview 中显示这些详细信息。我知道如何从服务中获取数据。但是当它涉及到 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();
}
}
}
}
【问题讨论】:
-
你遇到了什么问题
-
@ruben 它显示“PullInboxDetailsFailed”。
-
你可以尝试使用
postman请求甚至返回一些响应,我发现它什么也没返回 -
@ruben 对不起它的官方数据,所以我只给出了一个示例 url。如果我们给出 url 并输入,邮递员会返回 Output。我的 Json 输出 ``` { "data": { "comData": [ { "applicationDate": "08-01-2019", "idApplicationLogin": "AV32", "customerName": "Anu A", "stateName" :“喀拉拉邦”,“DeadLine”:“10-02-2020”,“readStatus”:“未读”},]}```
-
你在 logcat 中得到了什么?没有访问权限,我无法检查
标签: android json android-recyclerview okhttp webapi