使用AsyncTask在Android 4.2中使用JSON填充微调器
我目前正在尝试将我在API10中创建的应用程序的MainActivity的代码转换为API16。根据我所读到的,我必须开始使用ASyncTask来访问URI并在我的应用程序上显示信息。我在2.3中成功地做到了这一点,但在将其转换为JSON之后,我现在面临着一些障碍 本质上,该应用程序所做的是,它接受清单代码,即WAMF33000,点击按钮,微调器将填充该清单中包含的作业。由于我对ASyncTask的概念相当陌生,我想了解我的代码是如何应用于ASyncTask背后的理论的 根据我的代码,我有一些问题:- i) ASyncTask中的哪个代码是正在传递的参数? ii)我的任务的进度值是多少? iii)最后,我的返回值是ManifestItems的ArrayList吗 从2.3版本开始,我调用JSON两次——一次加载清单中的委托,第二次加载微调器中选择的委托的详细信息 以下是我的代码:使用AsyncTask在Android 4.2中使用JSON填充微调器,android,json,android-asynctask,android-spinner,Android,Json,Android Asynctask,Android Spinner,我目前正在尝试将我在API10中创建的应用程序的MainActivity的代码转换为API16。根据我所读到的,我必须开始使用ASyncTask来访问URI并在我的应用程序上显示信息。我在2.3中成功地做到了这一点,但在将其转换为JSON之后,我现在面临着一些障碍 本质上,该应用程序所做的是,它接受清单代码,即WAMF33000,点击按钮,微调器将填充该清单中包含的作业。由于我对ASyncTask的概念相当陌生,我想了解我的代码是如何应用于ASyncTask背后的理论的 根据我的代码,我有一些问
package com.signonglass;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.*;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
private final static String POD_URI = "http://192.168.0.105:8092/PodCore.svc";
private EditText evManifestCode;
private Spinner list_job;
private Button btnSubmit;
private String jobName;
private Button btnCons;
ArrayList<ManifestItemObj> jobList = new ArrayList<ManifestItemObj>();
ArrayList<ConsignmentItems> conItemList = new ArrayList<ConsignmentItems>();
Consignments retConsignment;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
evManifestCode = (EditText)findViewById(R.id.manifest);
btnSubmit = (Button)findViewById(R.id.btnSearchManifest);
list_job = (Spinner)findViewById(R.id.jobSpinner);
//tvView = (TextView)findViewById(R.id.deviceIdt);
//TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
//String IMEI_Number = telephonyManager.getDeviceId();
//tvView.setText("IMEI Number: " + IMEI_Number);
}
public class MyAsyncTask extends AsyncTask<String, Void, ArrayList<ManifestItemObj>>
{
protected void onPreExecute()
{
}
protected void onPostExecute(ArrayList<ManifestItemObj> jobList)
{
}
@Override
protected ArrayList<ManifestItemObj> doInBackground(String... params)
{
//http get request
HttpGet request = new HttpGet(POD_URI + "/getJobs/" + evManifestCode.getText().toString());
//set the hedear to get the data in JSON format
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
DefaultHttpClient client = new DefaultHttpClient();
String theString = new String("");
try
{
//get the response
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
is.close();
theString = builder.toString();
JSONObject jobsJSON = new JSONObject(theString);
JSONArray jobs = jobsJSON.getJSONArray("getJobsResult");
for(int i = 1; i < jobs.length(); i++)
{
JSONObject mit = jobs.getJSONObject(i);
ManifestItemObj mi = new ManifestItemObj();
mi.ManifestItemID = mit.getInt("ManifestItemID");
mi.JobType = mit.getString("JobType");
mi.FKID = mit.getInt("FKID");
jobList.add(mi);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return jobList;
}
}
/*private void showToast(String msg)
{
// TODO Auto-generated method stub
Toast.makeText(this, "Toast: " + msg, Toast.LENGTH_LONG).show();
}*/
public void onViewConsignment(View view)
{
ShowItemsOfManifest(retConsignment);
}
public Consignments getConsignmentManifest(String consignment)
{
Consignments con = new Consignments();
try
{
DefaultHttpClient client = new DefaultHttpClient();
String theString = new String("");
//http get request
HttpGet request = new HttpGet(POD_URI + "/getJobDetails/" + consignment);
//set the hedear to get the data in JSON format
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
//get the response
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
{
builder.append(line);
}
is.close();
theString = builder.toString();
JSONObject conJSON = new JSONObject(theString);
JSONArray cons = conJSON.getJSONArray("getJobDetailsResult");
for(int i = 0; i < cons.length(); i++)
{
JSONObject cObj = cons.getJSONObject(i);
con.ConsignmentID = cObj.getInt("ConsignmentID");
con.ConsignmentCreationDate = cObj.getString("ConsignmentCreationDate");
con.ConsignmentCustRef = cObj.getString("ConsignmentCustRef");
con.OrderNo = cObj.getString("OrderNo");
con.ConsignmentActive = cObj.getBoolean("ConsignmentActive");
con.JobType = cObj.getString("JobType");
//Client object
JSONObject clObj = cObj.getJSONObject("Client");
Clients cl = new Clients();
cl.ClientId = clObj.getInt("ClientID");
cl.ClientName = clObj.getString("ClientName");
con.Clients = cl;
//ShipTo object
JSONObject stObj = cObj.getJSONObject("ShipTo");
ShipTo sto = new ShipTo();
sto.ShipToId = stObj.getInt("ShipToId");
sto.ShipToName = stObj.getString("ShipToName");
sto.ShipToAddress1 = stObj.getString("ShipToAddress1");
sto.ShipToAddress2 = stObj.getString("ShipToAddress2");
sto.ShipToCity = stObj.getString("ShipToCity");
sto.ShipToPostcode = stObj.getString("ShipToPCode");
sto.ShipToState = stObj.getString("ShipToState");
con.ShipTo = sto;
//FreightZone object
JSONObject fzObj = cObj.getJSONObject("FreightZone");
FreightZones fz = new FreightZones();
fz.FreightZoneID = fzObj.getInt("FreightZoneId");
fz.FreightZone = fzObj.getString("FreightZone");
con.FreightZone = fz;
JSONArray conItems = cObj.getJSONArray("ConsignmentItems");
for(int m = 0; m < conItems.length(); m++)
{
JSONObject cit = conItems.getJSONObject(m);
ConsignmentItems ci = new ConsignmentItems();
ci.ConsignmentItemID = cit.getInt("ConsignmentItemsID");
ci.Quantity = cit.getInt("QTY");
//get Product from ConsignmentItems
JSONObject pro = cit.getJSONObject("Products");
Products prod = new Products();
prod.ProductId = pro.getInt("ProductID");
prod.ProductModel = pro.getString("ProductModel");
prod.ItemsPerCarton = pro.getInt("PerCarton");
prod.ProductDescription = pro.getString("Description");
prod.Height = (float) pro.getDouble("Height");
prod.Length = (float) pro.getDouble("Length");
prod.Width = (float) pro.getDouble("Width");
prod.Cubic = (float) pro.getDouble("Cubic");
ci.Product = prod;
conItemList.add(ci);
con.ConsignmentItems = conItemList;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return con;
}
public void ShowItemsOfManifest(Consignments consignments)
{
Bundle bundle = new Bundle();
Intent newIntent = new Intent(this.getApplicationContext(), ConActivity.class);
newIntent.putExtras(bundle);
newIntent.putExtra("Consignment", consignments);
this.startActivity(newIntent);
}
public void onSearchClick(View view)
{
new MyAsyncTask().execute();
ManifestItemAdapter mia = new ManifestItemAdapter(MainActivity.this, android.R.layout.simple_spinner_item, jobList);
list_job.setAdapter(mia);
}
}
package com.signonglass;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.*;
导入android.os.AsyncTask;
导入android.os.Build;
导入android.os.Bundle;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.content.Intent;
导入android.telephony.TelephonyManager;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemSelectedListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Spinner;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivity扩展了活动
{
私有最终静态字符串POD_URI=”http://192.168.0.105:8092/PodCore.svc";
私有编辑文本代码;
私有微调器列表\u作业;
私人按钮btnSubmit;
私有字符串jobName;
专用按钮;
ArrayList作业列表=新建ArrayList();
ArrayList conItemList=新的ArrayList();
托运货物;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
evManifestCode=(EditText)findViewById(R.id.manifest);
btnSubmit=(按钮)findViewById(R.id.btnSearchManifest);
list_job=(微调器)findViewById(R.id.jobSpinner);
//tvView=(TextView)findViewById(R.id.deviceIdt);
//TelephonyManager TelephonyManager=(TelephonyManager)this.getSystemService(Context.TELEPHONY_服务);
//字符串IMEI_Number=telephonyManager.getDeviceId();
//tView.setText(“IMEI编号:+IMEI_编号”);
}
公共类MyAsyncTask扩展了AsyncTask
{
受保护的void onPreExecute()
{
}
PostExecute上受保护的void(ArrayList作业列表)
{
}
@凌驾
受保护的ArrayList doInBackground(字符串…参数)
{
//http获取请求
HttpGet请求=新的HttpGet(POD_URI+“/getJobs/”+evManifestCode.getText().toString());
//设置hedear以获取JSON格式的数据
setHeader(“接受”、“应用程序/json”);
setHeader(“内容类型”、“应用程序/json”);
DefaultHttpClient=新的DefaultHttpClient();
字符串字符串=新字符串(“”);
尝试
{
//得到回应
HttpResponse response=client.execute(请求);
HttpEntity=response.getEntity();
InputStream=entity.getContent();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
StringBuilder=新的StringBuilder();
弦线;
而((line=reader.readLine())!=null){
builder.append(行);
}
is.close();
theString=builder.toString();
JSONObject jobsJSON=新的JSONObject(字符串);
JSONArray jobs=jobsJSON.getJSONArray(“getJobsResult”);
对于(int i=1;inew MyAsyncTask().execute(); // you would put params in here if needed such as a URL, String, etc...