我想从中返回一个值,AsyncTask
并且我想与该值进行比较,contents
如果它们匹配,我想将我从中获取的值发送AsyncTasnk
到另一个意图。我希望我解释得当。
这是代码:
public class MainActivity extends Activity {
Button ButtonClick;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txt = (TextView) findViewById(R.id.textView1);
ButtonClick = (Button) findViewById(R.id.qrReader);
ButtonClick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
startActivityForResult(intent, 0);
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
GetProduts GP = new GetProduts();
txt.setText(GP.execute().toString());
}
});
// Intent i = new Intent(MainActivity.this,IndexActivity.class);
// startActivity(i);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
final String contents = intent.getStringExtra("SCAN_RESULT");
//I want to compare this value
}
} else if (resultCode == RESULT_CANCELED) {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class GetProduts extends AsyncTask<ArrayList<Product>, Void, ArrayList<Product>> {
Product p;
final ArrayList<Product> productIdList = new ArrayList<Product>();
@Override
protected ArrayList<Product> doInBackground(ArrayList<Product>... params) {
HttpClient httpclient = new DefaultHttpClient();
GeneralConstans GC = new GeneralConstans();
// Products will be stated in memory
HttpPost httpget = new HttpPost(GC.UrlProducts);
HttpResponse response;
String result = null;
try {
HttpContext ctx = new BasicHttpContext();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2);
httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs,
"UTF-8"));
response = httpclient.execute(httpget, ctx);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity);
JSONArray arr = new JSONArray(result);
Gson gson = new Gson();
if (arr.length() > 0) {
for (int j = 0; j < arr.length(); j++) {
p = gson.fromJson(arr.getString(j), Product.class);
productIdList.add(p);
}
}
return productIdList;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (SocketException e) {
/*
* if (checkAbortStatus(e.getMessage()) == true) {
* handler.sendEmptyMessage(0); }
*/
} catch (IOException e) {
/*
* if (checkAbortStatus(e.getMessage()) == true) {
* handler.sendEmptyMessage(0); }
*/
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<Product> result) {
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
}
}