我正在android中构建一个应用程序来记录员工的出勤情况,该应用程序从二维码中读取人员代码并更新数据库中的一个表。
我有两个问题:
Buffer Error-Error conversion result java.lang.null.PointerException JSON Parser-Error parsing data org.json.JSONException: End of input ar character 0 of
这是MainActivity
package com.example.asistenciai;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MenuActivity extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single product url
private static final String url_update_attendance = "http://10.0.2.2/android_connect/update_attendance.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_COD = "cod";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
//1. LOCALIZAR CONTROLES
final Button btnRegistrar = (Button)findViewById(R.id.BtnRegistrar);
btnRegistrar.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
if(contents.equals("64572215")){
new ActualizarAsistencia().execute();
/*Intent i = new Intent(MenuActivity.this, ResultadoOkActivity.class);
startActivity(i);*/
}else{
Intent i = new Intent(MenuActivity.this, ResultadoFalloActivity.class);
startActivity(i);
}
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
class ActualizarAsistencia extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MenuActivity.this);
pDialog.setMessage("Saving attendance ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
//this is an example
String cod = "64572215";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(TAG_COD, cod));
JSONObject json = jsonParser.makeHttpRequest(url_update_attendance,
"POST", params);
// check json success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully updated
Intent i = getIntent();
// send result code 100 to notify about product update
setResult(100, i);
finish();
} else {
Intent i = new Intent(MenuActivity.this, ResultadoFalloActivity.class);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product uupdated
pDialog.dismiss();
}
}
}
这是 JSONParser 类
package com.example.asistenciai;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
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 == "GET"){
// request method is 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", "Errore converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Errore parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
日志猫
05-31 18:08:28.325: E/Buffer Error(25370): Errore converting result java.lang.NullPointerException
05-31 18:08:28.325: E/JSON Parser(25370): Errore parsing data org.json.JSONException: End of input at character 0 of
05-31 18:08:28.360: E/AndroidRuntime(25370): FATAL EXCEPTION: AsyncTask #1
05-31 18:08:28.360: E/AndroidRuntime(25370): java.lang.RuntimeException: An error occured while executing doInBackground()
05-31 18:08:28.360: E/AndroidRuntime(25370): at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-31 18:08:28.360: E/AndroidRuntime(25370): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-31 18:08:28.360: E/AndroidRuntime(25370): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-31 18:08:28.360: E/AndroidRuntime(25370): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-31 18:08:28.360: E/AndroidRuntime(25370): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-31 18:08:28.360: E/AndroidRuntime(25370): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-31 18:08:28.360: E/AndroidRuntime(25370): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-31 18:08:28.360: E/AndroidRuntime(25370): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-31 18:08:28.360: E/AndroidRuntime(25370): at java.lang.Thread.run(Thread.java:856)
05-31 18:08:28.360: E/AndroidRuntime(25370): Caused by: java.lang.NullPointerException
05-31 18:08:28.360: E/AndroidRuntime(25370): at com.example.asistenciai.MenuActivity$ActualizarAsistencia.doInBackground(MenuActivity.java:109)
05-31 18:08:28.360: E/AndroidRuntime(25370): at com.example.asistenciai.MenuActivity$ActualizarAsistencia.doInBackground(MenuActivity.java:1)
05-31 18:08:28.360: E/AndroidRuntime(25370): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-31 18:08:28.360: E/AndroidRuntime(25370): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-31 18:08:28.360: E/AndroidRuntime(25370): ... 5 more
如果你能帮助我,感激不尽!