http://androidhive.comからandroid login と resgitration というプロジェクトを作成しました。プロジェクトを実行すると、ローカル サーバーが更新されますが、logcat に次のエラーが表示されてアプリがクラッシュします。
..03-04 09:40:47.284: E/JSON(22069): nnnnnInside config{"tag":"register","success":1,"error":0,"uid":"51346c28f08575.36974266","user":{"name":"sdfds","email":"sfsd@gmail.com","created_at":"2013-03-04 01:40:56","updated_at":null}}n
03-04 09:40:47.306: E/JSON Parser(22069): Error parsing data org.json.JSONException: Value nnnnnInside of type java.lang.String cannot be converted to JSONObject
03-04 09:40:47.387: E/AndroidRuntime(22069): FATAL EXCEPTION: main
03-04 09:40:47.387: E/AndroidRuntime(22069): java.lang.NullPointerException
03-04 09:40:47.387: E/AndroidRuntime(22069): at com.androidhive.httprequests.RegisterActivity$1.onClick(RegisterActivity.java:60)
03-04 09:40:47.387: E/AndroidRuntime(22069): at android.view.View.performClick(View.java:4202)
03-04 09:40:47.387: E/AndroidRuntime(22069): at android.view.View$PerformClick.run(View.java:17340)
03-04 09:40:47.387: E/AndroidRuntime(22069): at android.os.Handler.handleCallback(Handler.java:725)
03-04 09:40:47.387: E/AndroidRuntime(22069): at android.os.Handler.dispatchMessage(Handler.java:92)
03-04 09:40:47.387: E/AndroidRuntime(22069): at android.os.Looper.loop(Looper.java:137)
03-04 09:40:47.387: E/AndroidRuntime(22069): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-04 09:40:47.387: E/AndroidRuntime(22069): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 09:40:47.387: E/AndroidRuntime(22069): at java.lang.reflect.Method.invoke(Method.java:511)
03-04 09:40:47.387: E/AndroidRuntime(22069): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-04 09:40:47.387: E/AndroidRuntime(22069): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-04 09:40:47.387: E/AndroidRuntime(22069): at dalvik.system.NativeStart.main(Native Method)
私のjsonコードは
enter code herepackage com.example.androidhive.library;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
public class JSONParser {
Context context;
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
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, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} 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;
}`enter code here`
}
スタックオーバーフローのすべての回答を読みましたが、何も取得できませんでした..plsは事前に感謝します
package com.androidhive.httprequests;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.androidhive.library.DatabaseHandler;
import com.example.androidhive.library.UserFunctions;
import com.example.androidhive.library.JSONParser;
public class RegisterActivity extends Activity {
Button btnRegister;
Button btnLinkToLogin;
EditText inputFullName;
EditText inputEmail;
EditText inputPassword;
TextView registerErrorMsg;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.registerName);
inputEmail = (EditText) findViewById(R.id.registerEmail);
inputPassword = (EditText) findViewById(R.id.registerPassword);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
registerErrorMsg = (TextView) findViewById(R.id.register_error);
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Toast.makeText(getBaseContext(), "Inside btnregister", Toast.LENGTH_SHORT).show();
String name = inputFullName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(name, email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully registred
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Registration Screen
finish();
}else{
// Error in registration
registerErrorMsg.setText("Error occured in registration");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
// Link to Login Screen
btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
Login.class);
startActivity(i);
// Close Registration View
finish();
}
});
}
}
ここに私の登録活動があります