我试图将Android应用程序中的意图存储到数据库中,并且我遇到一些问题将其转换为字符串并将其存储在数据库中。它连接到数据库并且id正在递增。
我已将意图存储在string.xml文件中,如下所示:
<string name="key_username">username</string>
<string name="key_pet_name">pet_name</string>
我从这样的另一种行为中获取了意图:
Intent i = newIntent(Hamster.this,Death.class); i.putExtra(getString(R.string.key_username),Username); i.putExtra(getString(R.string.key_pet_name), Petname);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
overridePendingTransition(0, 0);
startActivity(i);
这是我用来连接数据库的代码:
public class Death extends Activity {
public static String Petsname,Username ;
private ImageView dead;
private Button submit;
private TextView hamsters_death;
public static final String TAG = "Death";
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.death);
final Intent intent = getIntent();
submit = (Button) findViewById(R.id.button);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String Username = intent.getStringExtra(getString(R.string.key_username));
String Petname = intent.getStringExtra(getString(R.string.key_pet_name));
Log.d(TAG, Username);
Log.d(TAG,Petname);
new SummaryAsyncTask().execute(new FormData());
}
});
}
}
class SummaryAsyncTask extends AsyncTask<FormData, Void, JSONArray> {
Intent intent;
public String Username;
public String Petname;
public void postData(String Username, String Petname)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2:8888/Hamster_website/hamster.php");
try{
ArrayList<NameValuePair> form_items = new ArrayList<NameValuePair>(2);
form_items.add(new BasicNameValuePair("Username", Username));
form_items.add(new BasicNameValuePair("Petname", Petname));
httppost.setEntity(new UrlEncodedFormEntity(form_items));
HttpResponse response = httpclient.execute(httppost);
}
catch(Exception e)
{
Log.e("log_tag", "Error: " + e.toString());
}
}
protected JSONArray doInBackground(FormData... params) {
postData(Username, Petname);
return null;
}
}
class FormData {
public JSONArray PostForm() {
{
String url = "http://10.0.2.2:8888/Hamster_website/hamster.php";
HttpEntity httpEntity = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient(); // Default HttpClient
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
// Signals error in http protocol
e.printStackTrace();
//Log Errors Here
} catch (IOException e) {
e.printStackTrace();
}
// Convert HttpEntity into JSON Array
JSONArray jsonArray = null;
if (httpEntity != null) {
try {
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response : ", entityResponse);
jsonArray = new JSONArray(entityResponse);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
//JSONArray =
for (int i = 0; i < jsonArray.length(); i++) {
Log.d("JSON DEBUG", jsonArray.get(i).toString());
}
} catch (JSONException jse) {
jse.printStackTrace();
}
return jsonArray;
}
}
}