我一直打算将co.user_id传递给public static String USER_ID,以便字符串user_id中的数据可以从onPostExecute函数发送到另一个类。但是,从另一个类我得到null值,这意味着该值没有发送到该类。我应该如何将我的值存储在字符串中,以便该值也可以在另一个类中访问?
public class connect3 extends AsyncTask<String, Void, String> {
View view;
Activity activity;
public static String USER_ID = " ";
public connect3(Activity activity, View v) {
this.activity = activity;
view = v;
}
String convertStreamToString(InputStream is) {
try {
return new java.util.Scanner(is).useDelimiter("\\A").next();
} catch (java.util.NoSuchElementException e) {
return "";
}
}
protected String doInBackground(String... arg0) {
String ipAddress = "http://192.168.1.3/apexStore2/";
try {
URL url = new URL(ipAddress +"receipts.php");
String urlParameters =
URLEncoder.encode("user_name", "UTF-8") + "=" +
URLEncoder.encode(arg0[0], "UTF-8") + "&" +
URLEncoder.encode("user_id", "UTF-8") + "=" +
URLEncoder.encode("???", "UTF-8");
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
//System.out.println(response.toString());
JSONObject mainObject = new JSONObject(response.toString());
JSONArray uniObject = mainObject.getJSONArray("results");
for(int i = 0; i < uniObject.length(); i++) {
ContactReceipt co = new ContactReceipt();
JSONObject rowObject = uniObject.getJSONObject(i);
//EventObject co = new EventObject();
co.user_id = rowObject.getString("user_id");
USER_ID = co.user_id;
System.out.println("hi1" + co.user_id);
}
//To further break down JSON
//JSONObject oneObject = mainObject.getJSONObject("1");
//String id = oneObject.getJSONObject("id");
try{
}
finally{
connection.disconnect();
}
} catch (Exception e){
System.out.println(e.toString());
}
return USER_ID;
//return "";
}
protected void onPreExecute(){
}
@Override
protected void onPostExecute(String result){
// receipt1.user_id = user_id;
// String search = USER_ID;
// Intent intent = new Intent(activity.getApplication(),receipt1.class);
//intent.putExtra(USER_ID, search);
//activity.startActivity(intent);
Intent intent = new Intent(activity.getApplication(),receipt1.class);
intent.putExtra("USER_ID", result);
activity.startActivity(intent);
}
}
这就是我在另一个类中调用该值的方法。
Intent intent = getIntent();
String cats = intent.getStringExtra("USER_ID");
答案 0 :(得分:0)
由于有多个UserIds,您应该如何将其传递给您的活动,请参阅以下完整代码:
public class Connect3 extends AsyncTask<String, Void, List<String>> {
View view;
Activity activity;
public static String USER_ID = "yourpackagename.USER_ID";
private List<String> listUserIds;
public Connect3(Activity activity, View v) {
this.activity = activity;
view = v;
}
String convertStreamToString(InputStream is) {
try {
return new java.util.Scanner(is).useDelimiter("\\A").next();
} catch (java.util.NoSuchElementException e) {
return "";
}
}
protected List<String> doInBackground(String... arg0) {
String ipAddress = "http://192.168.1.3/apexStore2/";
try {
URL url = new URL(ipAddress +"receipts.php");
String urlParameters =
URLEncoder.encode("user_name", "UTF-8") + "=" +
URLEncoder.encode(arg0[0], "UTF-8") + "&" +
URLEncoder.encode("user_id", "UTF-8") + "=" +
URLEncoder.encode("???", "UTF-8");
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
//System.out.println(response.toString());
JSONObject mainObject = new JSONObject(response.toString());
JSONArray uniObject = mainObject.getJSONArray("result");
if(uniObject != null && uniObject.length() > 0) {
listUserIds = new ArrayList<>(uniObject.length());
for (int i = 0; i < uniObject.length(); i++) {
ContactReceipt co = new ContactReceipt();
JSONObject rowObject = uniObject.getJSONObject(i);
//EventObject co = new EventObject();
co.user_id = rowObject.getString("user_id");
System.out.println("hi1" + co.user_id);
listUserIds.add(co.user_id);
}
}
connection.disconnect();
} catch (Exception e){
System.out.println(e.toString());
}
return listUserIds;
}
protected void onPreExecute(){
}
@Override
protected void onPostExecute(List<String> result){
// receipt1.user_id = user_id;
Intent intent = new Intent(activity.getApplication(),receipt1.class);
if(result != null){
intent.putStringArrayListExtra(USER_ID, (ArrayList<String>) result);
}
activity.startActivity(intent);
}
}
你会在你的Activity类
中得到它if(getIntent() != null){
ArrayList<String> listUserIds = getIntent().getStringArrayListExtra(Connect3.USER_ID);
}
答案 1 :(得分:0)
在传递值之前检查
Intent intent = new Intent(activity.getApplication(),receipt1.class);
if( !result.equalsIgnoreCase("") && search !=null ){
intent.putExtra("USER_ID", result);
} else{
intent.putExtra("USER_ID", "result is null or empty");
}
并尝试获得像这样的价值
Intent intent = getIntent();
if(intent !=null){
String cats = intent.getStringExtra("USER_ID");
Log.e("value",cats);
// if cats prints "result is null or empty" that means problem inside your doinback.....
}