-1
我試圖從MySQL數據庫(我通過phpMyAdmin訪問)檢索數據並嘗試在EditText中顯示數據。我不知道我在哪裏犯了一個錯誤,但想要擺脫這個問題。使用Android從editext中檢索數據MySQL
日誌貓的錯誤表明沒有城市值。我知道我犯了錯,但我是Android新手。下面是我的代碼:
MainActivity.java
public class MainActivity extends Activity {
private ProgressDialog pDialog;
String id;
Button btn;
EditText txt1,txt2;
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String,String>> result;
private static final String url = "http://10.0.2.2/letstry/value.php";
private static final String tag_success = "success";
private static final String tag_cities = "cities";
private static final String tag_id = "id";
private static final String tag_city = "city_name";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.select);
result = new ArrayList<HashMap<String,String>>();
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new loadresult().execute();
}
});
}
class loadresult extends AsyncTask<String,String,String>
{
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", id));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url, "GET", params);
// check your log for json response
Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt(tag_success);
if (success == 1) {
// successfully received product details
JSONArray listObj = json
.getJSONArray(tag_cities); // JSON Array
// get first product object from JSON Array
JSONObject list = listObj.getJSONObject(0);
// product with this pid found
// Edit Text
txt1 = (EditText) findViewById(R.id.one1);
txt2 = (EditText) findViewById(R.id.two1);
// display product data in EditText
txt1.setText(list.getString(tag_id));
txt2.setText(list.getString(tag_city));
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
PHP腳本
<?php
$con = mysql_connect("localhost","root","12345");
$db = mysql_select_db("search",$con);
$query = mysql_query("Select * from cities where Id=2");
if(mysql_num_rows($query)>0)
{
while($result=mysql_fetch_array($query))
{
$list=array();
$list['Id']=$result['Id'];
$list['city_name']=$result['city_name'];
$response['list']=array();
array_push($response["list"], $list);
}
$response["success"] = 1;
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
閱讀'AsyncTask' –
你的意思是錯誤的日誌貓Asynctask? – Mikka
不,我想@kishore jethava想告訴你,你應該閱讀有關AsyncTask/Threads的文檔。在那裏你會發現它正在試圖做一些被禁止的事情,幾乎和被零除一樣被禁止。甚至更多禁止;-) ** TL; DR **:[鏈接](http://developer.android.com/guide/components/processes-and-threads.html)到文檔,尤其是「工作線程」部分, – 0X0nosugar