我在空闲时间学习android,我正在尝试使用MySQL DB
从android上的Json
收集数据,但我在处理线程时遇到问题。
这是主要的课程
package com.esoftarena.demotest;
public class DemoTestActivity extends Activity implements OnClickListener{
/* Called when the activity is first created. */
Intent intent;
Button home,repot, settings, remind, savedata;
EditText lecdeliver, lecbunked, comment;
TextView showpercent, showremark;
InputStream is = null;
static String json = "";
String url = "";
int id = 3, nodeliver = 0 , nobunk = 0;
JSONArray bunkInfo = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
home = (Button)findViewById(R.id.btnHome);
repot = (Button)findViewById(R.id.btnReport);
settings = (Button)findViewById(R.id.btnSettings);
remind = (Button)findViewById(R.id.btnRemind);
savedata = (Button)findViewById(R.id.btnsave);
lecdeliver = (EditText)findViewById(R.id.txtlecdeliver);
lecbunked = (EditText)findViewById(R.id.txtlecbunked);
comment = (EditText)findViewById(R.id.txtnote);
showpercent = (TextView)findViewById(R.id.lblshowpercent);
showremark = (TextView)findViewById(R.id.lblshowremark);
home.setOnClickListener(this);
repot.setOnClickListener(this);
settings.setOnClickListener(this);
remind.setOnClickListener(this);
savedata.setOnClickListener(this);
url = "http://www.test.com/getbunk.php?getreuslt=true&userno="+id;
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
bunkInfo = json.getJSONArray("bunkinfo");
// looping through All Contacts
for(int i = 0; i < bunkInfo.length(); i++){
JSONObject c = bunkInfo.getJSONObject(i);
// Storing each json item in variable
nodeliver = nodeliver + c.getInt("cl_delivered");
nobunk = nobunk + c.getInt("cl_bunked");
}
} catch (JSONException e) {
e.printStackTrace();
}
showpercent.setText(nodeliver);
showremark.setText(nobunk);
//Toast.makeText(this, "Welcome to page.", Toast.LENGTH_SHORT).show();
}
public void onClick(View clicked) {
if(clicked == home) {
//recipes = (ListView)findViewById(R.id.recipes);
Toast.makeText(this, "You have Clicked Home", Toast.LENGTH_SHORT).show();
Intent i = new Intent(DemoTestActivity.this, DemoTestHome.class);
DemoTestActivity.this.startActivity(i);
//Intent i = new Intent(SearchActivity.this, RecipeMethodActivity.class);
// SearchActivity.this.startActivity(i);
}else if(clicked == repot){
Toast.makeText(this, "You have Clicked Report", Toast.LENGTH_SHORT).show();
Intent i = new Intent(DemoTestActivity.this, DemoTestReport.class);
DemoTestActivity.this.startActivity(i);
}else if(clicked == settings ){
Toast.makeText(this, "You have Clicked Settings", Toast.LENGTH_SHORT).show();
Intent i = new Intent(DemoTestActivity.this, DemoTestSettings.class);
DemoTestActivity.this.startActivity(i);
}else if(clicked == remind ){
Toast.makeText(this, "You have Clicked Remind Me", Toast.LENGTH_SHORT).show();
Intent i = new Intent(DemoTestActivity.this, DemoTestRemind.class);
DemoTestActivity.this.startActivity(i);
}else if(clicked == savedata ){
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.test.com/addbunk.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("add_new_bunk", "true"));
nameValuePairs.add(new BasicNameValuePair("userno", "3"));
nameValuePairs.add(new BasicNameValuePair("lec_delivered", lecdeliver.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("lec_bunked", lecbunked.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("comment", comment.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
//content = response.getEntity().getContent();
// Toast.makeText(this, content , Toast.LENGTH_SHORT).show();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
JSONObject userObject = new JSONObject(json);
String msg = userObject.getString("msg");
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
} catch(Exception ex){
//don't forget this
}
}
}
}
这是JsonParser
package com.esoftarena.demotest;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
这是LogCat列表
02-04 17:41:33.296: D/AndroidRuntime(324): Shutting down VM
02-04 17:41:33.296: W/dalvikvm(324): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-04 17:41:33.386: E/AndroidRuntime(324): FATAL EXCEPTION: main
02-04 17:41:33.386: E/AndroidRuntime(324): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.esoftarena.demotest/com.esoftarena.demotest.DemoTestActivity}: java.lang.ClassCastException: android.widget.LinearLayout
02-04 17:41:33.386: E/AndroidRuntime(324): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-04 17:41:33.386: E/AndroidRuntime(324): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-04 17:41:33.386: E/AndroidRuntime(324): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-04 17:41:33.386: E/AndroidRuntime(324): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-04 17:41:33.386: E/AndroidRuntime(324): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 17:41:33.386: E/AndroidRuntime(324): at android.os.Looper.loop(Looper.java:123)
02-04 17:41:33.386: E/AndroidRuntime(324): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-04 17:41:33.386: E/AndroidRuntime(324): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 17:41:33.386: E/AndroidRuntime(324): at java.lang.reflect.Method.invoke(Method.java:521)
02-04 17:41:33.386: E/AndroidRuntime(324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-04 17:41:33.386: E/AndroidRuntime(324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-04 17:41:33.386: E/AndroidRuntime(324): at dalvik.system.NativeStart.main(Native Method)
02-04 17:41:33.386: E/AndroidRuntime(324): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
02-04 17:41:33.386: E/AndroidRuntime(324): at com.esoftarena.demotest.DemoTestActivity.onCreate(DemoTestActivity.java:60)
02-04 17:41:33.386: E/AndroidRuntime(324): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-04 17:41:33.386: E/AndroidRuntime(324): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-04 17:41:33.386: E/AndroidRuntime(324): ... 11 more
02-04 17:41:59.676: I/Process(324): Sending signal. PID: 324 SIG: 9
这是XML FILE
<?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" >
<TextView
android:id="@+id/lblwelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btnHome"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:text="@string/home" />
<Button
android:id="@+id/btnReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reports" />
<Button
android:id="@+id/btnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings" />
<Button
android:id="@+id/btnRemind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/remindme" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/lbldelivered"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lecdelivered"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/lblbunked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:text="@string/lecturebunk"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/txtlecdeliver"
android:layout_width="127dp"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/txtlecbunked"
android:layout_width="132dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/lblpercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.41"
android:text="@string/curpercent"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lblshowpercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.41"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/lblremark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.74"
android:text="@string/lblremark"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lblshowremark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.74"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout5"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/lblyournote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.29"
android:text="@string/note"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/txtnote"
android:layout_width="189dp"
android:layout_height="78dp"
android:layout_weight="1.10"
android:inputType="textPostalAddress|textShortMessage" />
</LinearLayout>
<Button
android:id="@+id/btnsave"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:text="@string/save" />
</LinearLayout>
谁能告诉我这是什么问题?