나는 안드로이드를 처음 사용합니다. 나는 로컬 서버와 연결하는 안드로이드 애플리케이션을 만들고있다. 사용되는 서버는 xampp 3.2.1입니다. json 파서를 사용하여 데이터베이스에 대한 crud 연산을 수행했습니다. json 파서 클래스를 정의했습니다. 사용자 필드를 등록한 후, 활동을 가입하려고에xampp 서버를 사용하는 android (api19) 응용 프로그램의 로컬 데이터베이스 연결 오류
11-02 17:47:46.236: E/IMGSRV(16540): :0: PVRDRMOpen: TP3, ret = 43
11-02 17:47:46.236: E/IMGSRV(16540): :0: PVRDRMOpen: TP3, ret = 49
11-02 17:48:07.356: E/IMGSRV(16540): :0: PVRDRMOpen: TP3, ret = 43
11-02 17:48:07.356: E/IMGSRV(16540): :0: PVRDRMOpen: TP3, ret = 49
11-02 17:48:07.396: I/dalvikvm(16540): Total arena pages for JIT: 11
11-02 17:48:07.396: I/dalvikvm(16540): Total arena pages for JIT: 12
11-02 17:48:07.406: I/dalvikvm(16540): Total arena pages for JIT: 13
11-02 17:48:07.406: I/dalvikvm(16540): Total arena pages for JIT: 14
11-02 17:48:07.406: I/dalvikvm(16540): Total arena pages for JIT: 15
11-02 17:48:07.406: I/dalvikvm(16540): Total arena pages for JIT: 16
11-02 17:48:07.406: I/dalvikvm(16540): Total arena pages for JIT: 17
11-02 17:48:07.406: I/dalvikvm(16540): Total arena pages for JIT: 18
11-02 17:48:07.416: I/dalvikvm(16540): Total arena pages for JIT: 19
11-02 17:48:07.416: I/dalvikvm(16540): Total arena pages for JIT: 20
11-02 17:48:07.416: I/dalvikvm(16540): Total arena pages for JIT: 21
는 불행하게도 앱이 중지 말한다 : 난 내 주요 활동을 시작할 때
은 내가 (여기 mainactivity 내 로그 캣입니다) 다음과 같은 오류를 얻을. 여기 signup 활동에 대한 logcat 오류가 있습니다.
11-02 17:51:34.176: E/JSON Parser(16540): Error parsing data org.json.JSONException: End of input at character 0 of
11-02 17:51:34.176: E/AndroidRuntime(16540): FATAL EXCEPTION: AsyncTask #4
11-02 17:51:34.176: E/AndroidRuntime(16540): java.lang.RuntimeException: An error occured while executing doInBackground()
11-02 17:51:34.176: E/AndroidRuntime(16540): at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-02 17:51:34.176: E/AndroidRuntime(16540): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
11-02 17:51:34.176: E/AndroidRuntime(16540): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
11-02 17:51:34.176: E/AndroidRuntime(16540): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
11-02 17:51:34.176: E/AndroidRuntime(16540): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-02 17:51:34.176: E/AndroidRuntime(16540): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-02 17:51:34.176: E/AndroidRuntime(16540): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-02 17:51:34.176: E/AndroidRuntime(16540): at java.lang.Thread.run(Thread.java:841)
11-02 17:51:34.176: E/AndroidRuntime(16540): Caused by: java.lang.NullPointerException
11-02 17:51:34.176: E/AndroidRuntime(16540): at com.example.ddubusmarg.SignUpActivity$CreateNewProduct.doInBackground(SignUpActivity.java:126)
11-02 17:51:34.176: E/AndroidRuntime(16540): at com.example.ddubusmarg.SignUpActivity$CreateNewProduct.doInBackground(SignUpActivity.java:1)
11-02 17:51:34.176: E/AndroidRuntime(16540): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-02 17:51:34.176: E/AndroidRuntime(16540): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-02 17:51:34.176: E/AndroidRuntime(16540): ... 4 more
11-02 17:51:34.676: E/WindowManager(16540): Activity com.example.ddubusmarg.SignUpActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{21c67e98 V.E..... R......D 0,0-684,192} that was originally added here
11-02 17:51:34.676: E/WindowManager(16540): android.view.WindowLeaked: Activity com.example.ddubusmarg.SignUpActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{21c67e98 V.E..... R......D 0,0-684,192} that was originally added here
11-02 17:51:34.676: E/WindowManager(16540): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:371)
11-02 17:51:34.676: E/WindowManager(16540): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
11-02 17:51:34.676: E/WindowManager(16540): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-02 17:51:34.676: E/WindowManager(16540): at android.app.Dialog.show(Dialog.java:281)
11-02 17:51:34.676: E/WindowManager(16540): at com.example.ddubusmarg.SignUpActivity$CreateNewProduct.onPreExecute(SignUpActivity.java:93)
11-02 17:51:34.676: E/WindowManager(16540): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
11-02 17:51:34.676: E/WindowManager(16540): at android.os.AsyncTask.execute(AsyncTask.java:534)
11-02 17:51:34.676: E/WindowManager(16540): at com.example.ddubusmarg.SignUpActivity$1.onClick(SignUpActivity.java:73)
11-02 17:51:34.676: E/WindowManager(16540): at android.view.View.performClick(View.java:4401)
11-02 17:51:34.676: E/WindowManager(16540): at android.view.View$PerformClick.run(View.java:18184)
11-02 17:51:34.676: E/WindowManager(16540): at android.os.Handler.handleCallback(Handler.java:730)
11-02 17:51:34.676: E/WindowManager(16540): at android.os.Handler.dispatchMessage(Handler.java:92)
11-02 17:51:34.676: E/WindowManager(16540): at android.os.Looper.loop(Looper.java:150)
11-02 17:51:34.676: E/WindowManager(16540): at android.app.ActivityThread.main(ActivityThread.java:5390)
11-02 17:51:34.676: E/WindowManager(16540): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 17:51:34.676: E/WindowManager(16540): at java.lang.reflect.Method.invoke(Method.java:525)
11-02 17:51:34.676: E/WindowManager(16540): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-02 17:51:34.676: E/WindowManager(16540): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-02 17:51:34.676: E/WindowManager(16540): at dalvik.system.NativeStart.main(Native Method)
내 노트북과 모바일은 모두 동일한 네트워크에 있습니다. 노트북의 IP 주소로 로컬 서버에 연결하고 있습니다.
여기 내 jsonparser 클래스입니다. package com.example.ddubusmarg; 여기
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 mehtod
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");
//}
while ((line = reader.readLine()) != null)
{
if(line.startsWith("{"))
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;
}
}
는 signupActivity.java입니다 : 패키지 com.example.ddubusmarg;
<?php
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_GET['NAME']) && isset($_GET['ID']) && isset($_GET['PASSWORD']) && isset($_GET['CITY']) && isset($_GET['PHONE NUMBER'])) {
$name = $_GET['NAME'];
$id = $_GET['ID'];
$password = $_GET['PASSWORD'];
$city = $_GET['CITY'];
$phone_number = $_GET['PHONE_NUMBER'];
// include db connect class
require_once __DIR__ . '/connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO registration(name, id, password, city, phone_number) VALUES('$name', '$id', '$password', '$city', '$phone_number')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "record successfuly inserted";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
이 친절하게 도와 : 여기
public class SignUpActivity extends Activity implements OnItemSelectedListener {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputId;
EditText inputPassword;
EditText inputCity;
EditText inputPhoneNumber;
// url to create new product
private static String url_create_product = "http://192.168.1.6/registrationjson/insertnew.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
//spinner creation
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.boarding_city, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
// Edit Text
inputName = (EditText) findViewById(R.id.editText1); // name
inputId = (EditText) findViewById(R.id.editText2); //id
inputPassword = (EditText) findViewById(R.id.editText3); //password
//inputCity = (EditText) findViewById(R.id.spinner); boarding city
inputPhoneNumber = (EditText) findViewById(R.id.editText4); // phone number
// Create button
Button btnCreateProduct = (Button) findViewById(R.id.button1);
// button click event
btnCreateProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new CreateNewProduct().execute();
}
});
}
/**
* Background Async Task to Create new product
* */
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SignUpActivity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
//again spinner
protected String doInBackground(String... args) {
String name = inputName.getText().toString();
String id = inputId.getText().toString();
String password = inputPassword.getText().toString();
Spinner spinner = (Spinner) findViewById(R.id.spinner);
String city = spinner.getSelectedItem().toString();
String phoneNumber = inputPhoneNumber.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("id", id));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("city", city));
params.add(new BasicNameValuePair("phoneNumber", phoneNumber));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"GET", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} 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 done
pDialog.dismiss();
}
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
는 insertnewrecord.php입니다.
모바일 브라우저의 IP를 확인 않았다 : 당신의 스택 추적과 같이 당신은 즉각적인 문제는 내가 SignUpActivity.java 라인 (126)을보고 시작 것의
NullPointerException
입니까? 지정된 줄에 – Muthu