2013-10-28 57 views
0

我只是不知道如何解決這個錯誤。並且它還在日誌cat中顯示錯誤「Error Parsing Data.org.json.JSONException:字符0處的輸入結束」。我沒有基本的Android和所有。但我真的需要這個。有人可以幫助我嗎?錯誤轉換result.java.lang.NullPointerException

這裏是我的JSONParser.java

public class JSONParser { 
static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

public JSONParser(){ 

} 

public JSONObject getJSONFromUrl(final String url) 
{ 
    try 
    { 
     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 
    { 
     jObj = new JSONObject(json); 
    } 
    catch(JSONException e) 
    { 
     Log.e("JSON Parser", "Error Parsing Data " + e.toString()); 
    } 

    return jObj; 
} 

    public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params){ 
     try{ 
      if(method.equals("POST")){ 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(url); 
       httpPost.setEntity(new UrlEncodedFormEntity(params)); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 

      }else if(method.equals("GET")){ 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       String paramString = URLEncodedUtils.format(params, "utf-8"); 
       url += "?" + paramString; 
       HttpGet httpGet = new HttpGet(url); 

       HttpResponse httpResponse = httpClient.execute(httpGet); 
       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{ 
       jObj = new JSONObject(json); 
      }catch(JSONException e){ 
       Log.e("JSON Parser", "Error Parsing Data." + e.toString()); 
      } 

      return jObj; 

} 
} 

,這裏是我的login.java

public class Login extends Activity implements OnClickListener{ 

private EditText user, pass; 
private Button mSubmit; 
private TextView mRegister; 

private ProgressDialog pDialog; 

JSONParser jsonParser = new JSONParser(); 

private static final String LOGIN_URL = "http://10.111.70.109/webservice/register.php"; 

private static final String TAG_SUCCESS = "success"; 
private static final String TAG_MESSAGE = "message"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 

    user = (EditText)findViewById(R.id.username); 
    pass = (EditText)findViewById(R.id.password); 

    mSubmit = (Button)findViewById(R.id.btnLogin); 
    mRegister = (TextView)findViewById(R.id.link_to_register); 

    mSubmit.setOnClickListener(this); 
    mRegister.setOnClickListener(this); 
} 

@Override 
public void onClick(View v){ 
    switch (v.getId()) { 
    case R.id.btnLogin: 
     new AttemptLogin().execute(); 
     break; 
    case R.id.link_to_register: 
     Intent i = new Intent(getApplicationContext(), SignUp.class); 
     startActivity(i); 
     break; 
    default: 
     break; 
    } 
} 

class AttemptLogin extends AsyncTask<String, String, String> 
{ 
    boolean failure = false; 

    @Override 
    protected void onPreExecute(){ 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(Login.this); 
     pDialog.setMessage("Please Wait. Attempting Login.."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String...args){ 
     int success; 
     String username = user.getText().toString(); 
     String password = pass.getText().toString(); 
     try{ 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("username", username)); 
      params.add(new BasicNameValuePair("password", password)); 
      Log.d("Request!", "Starting"); 

      JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params); 
      Log.d("Login Attempt..", json.toString()); 

      success = json.getInt(TAG_SUCCESS); 
      if(success==1){ 
       Log.d("Login Successful!", json.toString()); 
       Intent i = new Intent(Login.this, MainActivity.class); 
       finish(); 
       startActivity(i); 
       return json.getString(TAG_MESSAGE); 
      }else{ 
       Log.d("Login Failure!", json.getString(TAG_MESSAGE)); 
       return json.getString(TAG_MESSAGE); 
      } 
     }catch(JSONException e){ 
      e.printStackTrace(); 
     } 

     return null; 
    } 
} 

protected void onPostExecute(String file_url){ 
    pDialog.dismiss(); 
    if(file_url != null){ 
     Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show(); 
    } 
} 

} 

,這裏是我的登錄PHP腳本:

<?php 
require("config.inc.php"); 

if(!empty($_POST)) 
{ 
    $query = "SELECT username, email, password FROM students WHERE username =:username"; 
    $query_params = array(':username'=>$_POST['username']); 

    try 
    { 
     $stmt = $db->prepare($query); 
     $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    { 
     $response["success"] = 0; 
     $response["message"] = "Error. Please Try Again."; 
     die(json_encode($response)); 
    } 
    $validated_info = false; 

    $row = $stmt->fetch(); 
    if($row) 
    { 
     if($_POST['password']===$row['password']) 
     { 
      $login_ok = true; 
     } 
    } 
    if($login_ok) 
    { 
     $response["success"] = 1; 
     $response["message"] = "Login Successful!"; 
     die(json_encode($response)); 
    }else{ 
     $response["success"] = 0; 
     $response["message"] = "Sorry. Please Try Again."; 
     die(json_encode($response)); 
    } 
}else{ 
?> 

任何人可以幫助我?我真的很感激。

+1

發佈的logcat – Raghunandan

+0

請告訴我您是從Web服務得到的返回值? – Arun

+0

@在我的網頁瀏覽器中,所有的過程都可以正常工作。但是當我在Android上運行。這個緩衝區和解析錯誤的錯誤出來了。 –

回答

0

你沒有關閉最後 }其他{

put: 

}else{ 
$response["success"] = 0; 
    $response["message"] = "Sorry. Please Try Again."; 
    die(json_encode($response)); 
} 
?>