我的简单Query DB程序通过JSON到PHP - MySQL DB。
我收到了一条消息: 显示错误!android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。
当我做查询时。
我已尝试并修改程序,使用EditText输入branchname,通过JSON查询到php的结果是可以的。
但是在从EditText更改为Spindle之后,我得到了上面的消息。
有人可以提供帮助吗?非常感谢。
public class Querybybranch extends Activity implements OnClickListener { private ProgressDialog pDialog; JSONParser jsonParser = new JSONParser(); Spinner spinner3; Button submit5; TextView tv5; // TextView to show the result of MySQL query String branchname = null ; String returnString; // to store the result of MySQL query after decoding JSON private static String url_queryname = "http://vciwhereabout.ddns.net/waboutbybranch.php"; private static String TAG_SUCCESS = "success"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites().detectNetwork() .penaltyLog().build()); super.onCreate(savedInstanceState); setContentView(R.layout.activity_jsonuse5); Spinner spinner3 = (Spinner) findViewById(R.id.spinner3); ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this, R.array.branchtypearray, android.R.layout.simple_spinner_item); adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner3.setAdapter(adapter3); addListenerOnSpinnerItemSelection(); branchname = spinner3.getSelectedItem().toString(); submit5 = (Button) findViewById(R.id.btnsubmit5); tv5 = (TextView) findViewById(R.id.showresult5); submit5.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { new AttemptQueryBranchName().execute(); } }); } public void addListenerOnSpinnerItemSelection() { spinner3 = (Spinner) findViewById(R.id.spinner3); spinner3.setOnItemSelectedListener(new CustomOnItemSelectedListener()); } public void addListenerOnButton() { spinner3 = (Spinner) findViewById(R.id.spinner3); spinner3.setOnItemSelectedListener(new CustomOnItemSelectedListener()); } class AttemptQueryBranchName extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(Querybybranch.this); pDialog.setMessage("Attempting query..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @Override protected String doInBackground(String... args) { ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); postParameters.add(new BasicNameValuePair("branchname", branchname)); // postParameters.add(new BasicNameValuePair("branchname", branchname.getText().toString())); String response = null; try{ response = CustomHttpClient.executeHttpPost( "http://vciwhereabout.ddns.net/waboutbybranch.php", postParameters); String result = response.toString(); //parse json data try{ returnString = ""; JSONObject object = new JSONObject(result); JSONArray jArray = object.getJSONArray("posts"); for (int i = 0; i < jArray.length(); i++) { JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","post_id: "+json_data.getInt("post_id")+ ", username: "+json_data.getString("username")+ ", name: "+json_data.getString("name")+ ", division: "+json_data.getString("division")+ ", branchname: "+json_data.getString("branchname")+ ", fdate: "+json_data.getString("fdate")+ ", tdate: "+json_data.getString("tdate")+ ", ftime: "+json_data.getString("ftime")+ ", ttime: "+json_data.getString("ttime")+ ", type_name: "+json_data.getString("type_name")+ ", note: "+json_data.getString("note")+ ", custbranch: "+json_data.getString("custbranch")+ ", custname: "+json_data.getString("custname")+ ", custaddr: "+json_data.getString("custaddr")+ ", custcity: "+json_data.getString("custcity") ); //Get an output to the screen returnString += "\n" + "Post ID :" + "\t" + json_data.getString("post_id") + "\n" + "User-id :" + json_data.getString("username") + "\n" + "Name :" + "\t" + json_data.getString("name") + "\n" + "Division :" + "\t" + json_data.getString("division") + "\n" + "HO/Branch :" + json_data.getString("branchname") + "\n" + "From Date :" + json_data.getString("fdate") + "\n" + "To Date :" + json_data.getString("tdate") + "\n" + "From Time :" + json_data.getString("ftime") + "\n" + "To Time :" + json_data.getString("ttime") + "\n" + "Req.Type: :" + json_data.getString("type_name") + "\n" + "Note :" + json_data.getString("note") + "\n" + "Cust/Br. :" + json_data.getString("custbranch") + "\n" + "Cust.Name:" + json_data.getString("custname") + "\n" + "Address :" + json_data.getString("custaddr") + "\n" + "City :" + json_data.getString("custcity")+ "\n" + "------------------------" + "\n" ; } } catch(JSONException e){ Log.e("log_tag", "Error parsing data "+e.toString()); returnString += "No Records found for this Query" ; } try{ tv5.setText(returnString); } catch(Exception e){ Log.e("log_tag","Error in Display!" + e.toString());; } } catch (Exception e) { Log.e("log_tag","Error in http connection!!" + e.toString()); } if (response == "null") { Toast.makeText(Querybybranch.this, "branchname not found", Toast.LENGTH_LONG).show(); } else {} return null; // return response; } protected void onPostExecute(String file_url) { // dismiss the dialog once product deleted pDialog.dismiss(); if (file_url != null) { Toast.makeText(Querybybranch.this, file_url, Toast.LENGTH_LONG).show(); } // else { // Toast.makeText(Querynameentry21.this, "User not found", Toast.LENGTH_LONG).show(); // } } } @Override public void onClick(View v) { // TODO Auto-generated method stub } }
这是主轴阵列:
> <string-array name="branchtypearray">
> <item>HO VCI</item>
> <item>Cabang Jakarta 1</item>
> <item>Cabang Jakarta 2</item>
> <item>Cabang Bandung</item>
> <item>Cabang Semarang</item>
> <item>Cabang Surabaya</item>
> <item>Cabang Denpasar</item>
> <item>Cabang REGIONAL BARAT</item>
> <item>Cabang REGIONAL TIMUR</item>
> <item>Pabrik</item>
> <item>HO 3C</item>
> </string-array>
加上活动的布局:
> <?xml version="1.0" encoding="utf-8"?> <LinearLayout
> xmlns:android="http://schemas.android.com/apk/res/android"
> android:layout_width="fill_parent"
> android:layout_height="fill_parent"
> android:orientation="vertical" >
>
> <!-- Cust/Branch Namelabel -->
> <TextView
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:textAppearance="?android:attr/textAppearanceLarge"
> android:textColor="#43bd00"
> android:textSize="20sp"
> android:text="Branch Name to be Query" />
>
> <Spinner
> android:id="@+id/spinner3"
> android:layout_width="match_parent"
> android:layout_height="wrap_content"
> android:entries="@array/branchtypearray"
> />
> <Button
> android:id="@+id/btnsubmit5"
> android:layout_width="wrap_content"
> android:layout_height="wrap_content"
> android:text="Submit"
> android:layout_gravity="center"
> /> <LinearLayout
> android:id="@+id/box"
> android:layout_width="match_parent"
> android:layout_height="wrap_content"
> android:layout_margin="1dp"
> android:orientation="vertical"
> android:padding="1dp" >
>
> <ImageView
> android:id="@+id/imageView1"
> android:layout_width="wrap_content"
> android:layout_height="wrap_content"
> android:src="@drawable/logo3s"
> android:gravity="center_horizontal"
> />
>
> <ScrollView
> android:layout_width="fill_parent"
> android:lines="20"
> android:layout_height="390dip" >
> <TextView
> android:id="@+id/showresult5"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:singleLine="false"
> android:textAppearance="?android:attr/textAppearanceLarge"
>
> android:paddingBottom="2dip"
> android:paddingLeft="45dip"
> android:textColor="#5d5d5d"
> />
> </ScrollView> </LinearLayout> </LinearLayout>
输出屏幕只是空白&#39;没有出现。
当我使用EditText(更改为&#34;主轴&#34;并且遇到问题)时,PHP和数据库正常并且测试结果良好。
答案 0 :(得分:0)
按照Mike M的建议,在postExecute上移动了以下语句后,问题解决了。非常感谢Mike M。
> try{
> tv5.setText(returnString);
> }
> catch(Exception e){
> Log.e("log_tag","Error in Display!" + e.toString());;
}