2014-03-04 21 views
-1

我試過這段代碼,它在運行模擬器時沒有顯示任何錯誤。 在點擊事件過程中,我的模擬器不幸中止。無法獲取JSON值工作的http post方法。 Android系統。建議請。

logcat的

03-04 08:46:17.633: E/AndroidRuntime(1391): FATAL EXCEPTION: AsyncTask #1 
03-04 08:46:17.633: E/AndroidRuntime(1391): Process: com.example.childprofile, PID: 1391 
03-04 08:46:17.633: E/AndroidRuntime(1391): java.lang.RuntimeException: An error occured while executing doInBackground() 
03-04 08:46:17.633: E/AndroidRuntime(1391):  at android.os.AsyncTask$3.done(AsyncTask.java:300) 
03-04 08:46:17.633: E/AndroidRuntime(1391):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
03-04 08:46:17.633: E/AndroidRuntime(1391):  at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
03-04 08:46:17.633: E/AndroidRuntime(1391):  at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
03-04 08:46:17.633: E/AndroidRuntime(1391):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
03-04 08:46:17.633: E/AndroidRuntime(1391):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
03-04 08:46:17.633: E/AndroidRuntime(1391):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
03-04 08:46:17.633: E/AndroidRuntime(1391):  at java.lang.Thread.run(Thread.java:841) 
03-04 08:46:17.633: E/AndroidRuntime(1391): Caused by: java.lang.NullPointerException 
03-04 08:46:17.633: E/AndroidRuntime(1391):  at com.example.childprofile.ChildProfile$sendPostData.doInBackground(ChildProfile.java:123) 
03-04 08:46:17.633: E/AndroidRuntime(1391):  at com.example.childprofile.ChildProfile$sendPostData.doInBackground(ChildProfile.java:1) 
03-04 08:46:17.633: E/AndroidRuntime(1391):  at android.os.AsyncTask$2.call(AsyncTask.java:288) 
03-04 08:46:17.633: E/AndroidRuntime(1391):  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
03-04 08:46:17.633: E/AndroidRuntime(1391):  ... 4 more 
03-04 08:46:20.123: I/Choreographer(1391): Skipped 59 frames! The application may be doing too much work on its main thread. 

PHP文件

<?php 
$con=mysql_connect("localhost","arun","sachin11"); 
$rows = array(); 
$db_select = mysql_select_db('Schoolapp', $con); 
$username = $_GET['username']; 
$password = $_GET['password']; 
$result = mysql_query("SELECT ChildPassportName,ChildID FROM SchoolDB where Username='$username' and Password='$password'",$con); 
while($r = mysql_fetch_assoc($result)) { 
    $rows[] = $r; 
} 
print json_encode($rows); 

mysql_close($con); 
?> 

這是MyPhp輸出傳入值之後:

[{"ChildPassportName":"Arun","ChildID":"0"}] 

米亞瓦文件

package com.example.childprofile; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.UnsupportedEncodingException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.URLEncoder; 

import org.apache.http.client.ClientProtocolException; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class ChildProfile extends Activity { 
    private TextView childname,childid; 

    private Button get; 
    private EditText username,password; 
    private JSONObject jObj; 
    private static String user,pass,json,child,id; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.child_profile); 
     username=(EditText) findViewById(R.id.profile_username); 
     password=(EditText) findViewById(R.id.profile_password); 
     childname=(TextView) findViewById(R.id.profile_email); 
     childid=(TextView) findViewById(R.id.profile_childid); 
     get=(Button) findViewById(R.id.profile_button1); 
     get.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       new sendPostData().execute(); 
      } 
     }); 
    } 
    private class sendPostData extends AsyncTask<String, Void, String> 
    { 

      @Override 
      protected void onPostExecute(String result) { 
       //View your result here. 
        try { 
        jObj = new JSONObject(result); 
       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

        try { 
        child=jObj.getString("ChildPassportName"); 
       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
        try { 
        id=jObj.getString("ChildID"); 
       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
        childname.setText(child); 
        childid.setText(id); 

       } 
     @Override 
     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      StringBuilder sb = null; 
      try 
      { 
      user=username.getText().toString(); 
      pass=password.getText().toString(); 
      String link="http://192.168.1.22:81/arun.php?"; 
      String data = URLEncoder.encode("username", "UTF-8") 
      + "=" + URLEncoder.encode(user, "UTF-8"); 
      data += "&" + URLEncoder.encode("password", "UTF-8") 
      + "=" + URLEncoder.encode(pass, "UTF-8"); 
      URL url = new URL(link); 
      URLConnection conn = url.openConnection(); 
      conn.setDoOutput(true); 
      OutputStreamWriter wr = new OutputStreamWriter 
      (conn.getOutputStream()); 
      wr.write(data); 
      wr.flush(); 
      BufferedReader reader = new BufferedReader 
      (new InputStreamReader(conn.getInputStream())); 
      sb = new StringBuilder(); 
      String line = null; 
      // Read Server Response 
      while((line = reader.readLine()) != null) 
      { 
       sb.append(line); 
       break; 
      } 



      } 
      catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      json=sb.toString(); 


      return json; 
     } 
    } 

    } 

回答

0

編輯:奇怪的雙重職位。 刪除我。

0

這應該解決您的問題。我簡化了一些部分的代碼。

private class SendPostData extends AsyncTask<String, Void, String> 
{ 
    private String user = null; 
    private String pass = null; 

    @Override 
    protected void onPreExecute() 
    { 
     this.user = username.getText().toString(); 
     this.pass = password.getText().toString(); 
    } 

    @Override 
    protected String doInBackground(String... params) 
    { 
     StringBuilder sb = new StringBuilder(); 
     try 
     { 
      String link="http://192.168.1.22:81/arun.php?"; 
      link += URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8"); 
      link += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(pass, "UTF-8"); 
      URL url = new URL(link); 
      URLConnection conn = url.openConnection(); 
      conn.setDoOutput(false); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      String line = null; 
      // Read Server Response 
      while((line = reader.readLine()) != null) 
      { 
       sb.append(line); 
       break; 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     return sb.toString(); 
    } 

    @Override 
    protected void onPostExecute(String result) 
    { 
     try 
     { 
      ChildProfile.this.json = result; 
      JSONObject jObj = new JSONObject(result); 

      if (jObj.has("ChildPassportName")) 
      { 
       String child = jObj.getString("ChildPassportName"); 
       ChildProfile.this.childname.setText(child); 
      } 
      if (jObj.has("ChildID")) 
      { 
       String id = jObj.getString("ChildID"); 
       ChildProfile.this.childid.setText(id); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
}