是的,我可以将值从父类传递给asynctask,但我只是在摆弄AsyncTask,它无法读取共享首选项,它正在读取null ..是的我用谷歌搜索它并在另一个线程上找到它stackoverflow,将那些共享首选项放在onPreexecute中,我已经完成了......但没有用 这是我的代码
public class Destination extends Activity {
EditText destinationPoint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_destination);
destinationPoint=(EditText)findViewById(R.id.dest);
Parse.initialize(this, "PyCnNKLWz4XAJAroDgLYy7GEi", "nDOBi74l8eGbGXQKBsteGLIkHKEnHkpAVMQ");
}
public void ridenow(View view){
SharedPreferences sharedPreferences =getSharedPreferences("AutoRaja",Context.MODE_PRIVATE);
SharedPreferences.Editor edit=sharedPreferences.edit();
edit.putString("destination", destinationPoint.getText().toString());
edit.commit();
String name=sharedPreferences.getString("customerName", "jj");
Log.d("name",name);
String phoneNumber=sharedPreferences.getString("customerNumber", "900");
Log.d("phone",phoneNumber);
String pickup=sharedPreferences.getString("pick", null);
Log.d("pickup",pickup);
String destination=sharedPreferences.getString("destination", null);
Log.d("destination",destination);
// sending to parse
ParseObject ob=new ParseObject("Customer");
ob.put("Name",name);
ob.put("phone", phoneNumber);
ob.put("pickup", pickup);
ob.put("destination", destination);
ob.saveInBackground();
Intent i=new Intent(this,Confirmation.class);
AsyncTaskRunner asyncTaskRunner=new AsyncTaskRunner();
asyncTaskRunner.execute();
startActivity(i);
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.destination, menu);
return true;
}
private class AsyncTaskRunner extends AsyncTask<Void,Void,Void>{
String customername;
String pickup;
String destination;
String message;
@Override
protected void onPreExecute() {
super.onPreExecute();
SharedPreferences sharedPreferences=getSharedPreferences("Autoraja",Context.MODE_PRIVATE);
customername=sharedPreferences.getString("customerName", null);
pickup=sharedPreferences.getString("pick", null);
destination=sharedPreferences.getString("destination", null);
message="hi "+customername+", you have successfully booked Auto from "+pickup+" to "+destination +"" ;
}
@Override
protected Void doInBackground(Void... params) {
String authkey = "5rBdfU2BhE53008ff8";
//Multiple mobiles numbers seperated by comma
String mobiles = "9788506486";
//Sender ID,While using route4 sender id should be 6 characters long.
String senderId = "AutoRJ";
//Your message to send, Add URL endcoding here.
//define route
String route="4";
//Prepare Url
URLConnection myURLConnection=null;
URL myURL=null;
BufferedReader reader=null;
//encoding message
String encoded_message=URLEncoder.encode(message);
//Send SMS API
String mainUrl="https://control.msg91.com/sendhttp.php?";
String url;
//Prepare parameter string
StringBuilder sbPostData= new StringBuilder();
sbPostData.append("authkey="+authkey);
sbPostData.append("&mobiles="+mobiles);
sbPostData.append("&message="+encoded_message);
sbPostData.append("&route="+route);
sbPostData.append("&sender="+senderId);
//final string
mainUrl += sbPostData.toString();
try
{
//prepare connection
myURL = new URL(mainUrl);
myURLConnection = myURL.openConnection();
myURLConnection.connect();
reader= new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
//reading response
String response;
while ((response = reader.readLine()) != null)
//print response
System.out.println(response);
//finally close connection
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
// TODO Auto-generated method stub
}
}
}
编辑:我使用了错误的共享首选项名称,但是这个问题可能对那些无法从异步任务访问共享首选项的人有用,doInBackground方法
答案 0 :(得分:4)
getSharedPreferences("Autoraja",Context.MODE_PRIVATE);
与
getSharedPreferences("AutoRaja",Context.MODE_PRIVATE);
找出差异:)