Java 在ListView中显示HTTPentity响应值
我需要在listview中显示HttpEntity响应值 这是我的密码Java 在ListView中显示HTTPentity响应值,java,android,json,Java,Android,Json,我需要在listview中显示HttpEntity响应值 这是我的密码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); // we will using AsyncTask during parsing new AsyncTaskP
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
// we will using AsyncTask during parsing
new AsyncTaskParseJson().execute();
}
// you can make this class as another java file so it will be separated from your main activity.
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
final String TAG = "AsyncTaskParseJson.java";
// contacts JSONArray
JSONArray dataJsonArr = null;
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(String... arg0) {
// post the specific format data to json url
Here am getting the response values
try {
HttpClient httpClient = new DefaultHttpClient();
JSONObject object = new JSONObject();
object.put("Username", "testUser@123");
object.put("Password", "testPassword@123");
JSONObject jsonObject = new JSONObject();
jsonObject.put("Authentication", object);
jsonObject.put("RequestType", 4);
HttpPost postMethod = new HttpPost("url");
postMethod.setEntity(new StringEntity(jsonObject.toString()));
postMethod.setHeader("Accept", "application/json");
postMethod.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(postMethod);
HttpEntity entity = response.getEntity();
String response_value = EntityUtils.toString(entity).toString();
// Log.e(TAG, response_value ); //display the output in logcat
if (entity != null) {
//Convert String to JSON Object
JSONObject result = new JSONObject(response_value);
JSONArray tokenList = result.getJSONArray("Files");
for(int i=0;i<=tokenList.length();i++)
{
JSONObject oj = tokenList.getJSONObject(i);
JSONObject oj1 = (JSONObject) tokenList.getJSONObject(i).get("Borrower");
JSONObject oj2 = (JSONObject) tokenList.getJSONObject(i).get("CoBorrower");
JSONObject oj3 = (JSONObject) tokenList.getJSONObject(i).get("LoanDetails");
JSONObject oj4 = (JSONObject) tokenList.getJSONObject(i).get("PropertyAddress");
String fileid = oj.getString("FileID");
String borrowername = oj1.getString("FirstName");
String coborrowername = oj2.getString("FirstName");
String loannumber = oj3.getString("LoanNumber");
String addrs1 = oj4.getString("Address1");
String city = oj4.getString("City");
Log.e(TAG, fileid + "/" + borrowername + "/"+ coborrowername + "/"+ addrs1 + "/"+ city + "/"+ loannumber );
JSONArray orders = oj.getJSONArray("Orders");
for(int n=0;n<orders.length();n++){
JSONObject oj5 = orders.getJSONObject(n);
String appid = oj5.getString("ApplicationOrderId");
String appid1 = oj5.getString("DueDate");
Log.e(TAG, appid +"/"+ appid1);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String strFromDoInBg) {}
}
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
//我们将在解析期间使用AsyncTask
新建AsyncTaskParseJson().execute();
}
//您可以将这个类作为另一个java文件,这样它将与您的主要活动分离。
公共类AsyncTaskParseJson扩展AsyncTask{
final String TAG=“AsyncTaskParseJson.java”;
//联系JSONArray
JSONArray dataJsonArr=null;
@凌驾
受保护的void onPreExecute(){}
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//将特定格式的数据发布到json url
这里我得到了响应值
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
JSONObject对象=新的JSONObject();
object.put(“用户名”testUser@123");
object.put(“密码”testPassword@123");
JSONObject JSONObject=新的JSONObject();
put(“身份验证”,对象);
jsonObject.put(“RequestType”,4);
HttpPost postMethod=新的HttpPost(“url”);
setEntity(新的StringEntity(jsonObject.toString());
setHeader(“接受”、“应用程序/json”);
setHeader(“内容类型”、“应用程序/json”);
HttpResponse response=httpClient.execute(postMethod);
HttpEntity=response.getEntity();
字符串响应_value=EntityUtils.toString(entity.toString();
//Log.e(TAG,response_value);//在logcat中显示输出
如果(实体!=null){
//将字符串转换为JSON对象
JSONObject结果=新的JSONObject(响应值);
JSONArray tokenList=result.getJSONArray(“文件”);
对于(int i=0;i创建一个模型类!并从中创建一个新实例并设置值,然后将该实例传递给适配器!
查看此链接了解如何创建模型类:
您知道如何使用列表视图吗?