EnregistrerActivity.java – 登録イベントを処理するアクティビティ
package tn.pack.ordre.enregistrer;
import org.json.JSONException;
import org.json.JSONObject;
import tn.pack.ordre.enregistrer.UserFunctions;
import tn.pack.ordre.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class EnregistrerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Fixe la mise en page d’une activité
setContentView(R.layout.inscription);
}
public void btn_valider(View v) {
String userName=((EditText)findViewById(R.id.et_un)).getText().toString();
String password=((EditText)findViewById(R.id.et_pw)).getText().toString();
String cin=((EditText) findViewById(R.id.et_cin)).getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(userName,cin,password);
TextView errorE = (TextView)findViewById(R.id.textViewerrer);
try {
if (json.getString("success") != null) {
String res = json.getString("success");
if (Integer.parseInt(res) == 1) {
errorE.setText("enregistrement ok");
} else {
errorE.setText("erreur pendant l'enregistrement");
}
}
} catch (JSONException e) {
e.printStackTrace();
System.out.println("btnRegister: "+e.toString());
}
}
public void Annuler(View v) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
}
}
UserFunctions.java - このクラスでは、すべての関数が JSONParser と対話します
package tn.pack.ordre.enregistrer;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
public class UserFunctions {
private JSONParser jsonParser;
private static String loginURL = "http://10.0.2.2/webservice/index.php";
private static String registerURL = loginURL;
private static String login_tag = "login";
private static String register_tag = "register";
// constructor
public UserFunctions(){
jsonParser = new JSONParser();
}
public JSONObject loginUser(String userName, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("userName", userName));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
return json;
}
/**
* function make Login Request
* @param userName
* @param cin
* @param password
* */
public JSONObject registerUser(String userName, String cin,String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", register_tag));
params.add(new BasicNameValuePair("userName", userName));
params.add(new BasicNameValuePair("cin", cin));
params.add(new BasicNameValuePair("password", password));
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
// return json
return json;
}
}
JSONParser.java - API 応答 JSON を解析します。
package tn.pack.ordre.enregistrer;
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.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() {
}
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));
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();
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;
}
}
errer logCat : E/JSON パーサー(261)
05-22 09:28:44.699: E/Buffer Error(261): Error converting result java.lang.NullPointerException
05-22 09:28:44.708: E/JSON Parser(261): Error parsing data org.json.JSONException: A JSONObject text must begin with '{' at character 0 of...
05-22 09:28:44.750: E/AndroidRuntime(261): Uncaught handler: thread main exiting due to uncaught exception
05-22 09:28:44.798: E/AndroidRuntime(261): java.lang.IllegalStateException: Could not execute method of the activity
05-22 09:28:44.798: E/AndroidRuntime(261): at android.view.View$1.onClick(View.java:2031)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.view.View.performClick(View.java:2364)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.view.View.onTouchEvent(View.java:4179)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.widget.TextView.onTouchEvent(TextView.java:6541)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.view.View.dispatchTouchEvent(View.java:3709)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-22 09:28:44.798: E/AndroidRuntime(261): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
05-22 09:28:44.798: E/AndroidRuntime(261): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
05-22 09:28:44.798: E/AndroidRuntime(261): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.os.Looper.loop(Looper.java:123)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-22 09:28:44.798: E/AndroidRuntime(261): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 09:28:44.798: E/AndroidRuntime(261): at java.lang.reflect.Method.invoke(Method.java:521)
05-22 09:28:44.798: E/AndroidRuntime(261): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-22 09:28:44.798: E/AndroidRuntime(261): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-22 09:28:44.798: E/AndroidRuntime(261): at dalvik.system.NativeStart.main(Native Method)
05-22 09:28:44.798: E/AndroidRuntime(261): Caused by: java.lang.reflect.InvocationTargetException
05-22 09:28:44.798: E/AndroidRuntime(261): at tn.pack.ordre.enregistrer.EnregistrerActivity.btn_valider(EnregistrerActivity.java:45)
05-22 09:28:44.798: E/AndroidRuntime(261): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 09:28:44.798: E/AndroidRuntime(261): at java.lang.reflect.Method.invoke(Method.java:521)
05-22 09:28:44.798: E/AndroidRuntime(261): at android.view.View$1.onClick(View.java:2026)
05-22 09:28:44.798: E/AndroidRuntime(261): ... 23 more
05-22 09:28:44.798: E/AndroidRuntime(261): Caused by: java.lang.NullPointerException
05-22 09:28:44.798: E/AndroidRuntime(261): ... 27 more
05-22 09:37:39.449: E/Buffer Error(354): Error converting result java.lang.NullPointerException
05-22 09:37:39.449: E/JSON Parser(354): Error parsing data org.json.JSONException: A JSONObject text must begin with '{' at character 0 of
05-22 09:37:39.459: E/AndroidRuntime(354): Uncaught handler: thread main exiting due to uncaught exception
05-22 09:37:39.499: E/AndroidRuntime(354): java.lang.IllegalStateException: Could not execute method of the activity
05-22 09:37:39.499: E/AndroidRuntime(354): at android.view.View$1.onClick(View.java:2031)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.view.View.performClick(View.java:2364)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.view.View.onTouchEvent(View.java:4179)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.widget.TextView.onTouchEvent(TextView.java:6541)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.view.View.dispatchTouchEvent(View.java:3709)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
05-22 09:37:39.499: E/AndroidRuntime(354): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
05-22 09:37:39.499: E/AndroidRuntime(354): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
05-22 09:37:39.499: E/AndroidRuntime(354): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.os.Looper.loop(Looper.java:123)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-22 09:37:39.499: E/AndroidRuntime(354): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 09:37:39.499: E/AndroidRuntime(354): at java.lang.reflect.Method.invoke(Method.java:521)
05-22 09:37:39.499: E/AndroidRuntime(354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-22 09:37:39.499: E/AndroidRuntime(354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-22 09:37:39.499: E/AndroidRuntime(354): at dalvik.system.NativeStart.main(Native Method)
05-22 09:37:39.499: E/AndroidRuntime(354): Caused by: java.lang.reflect.InvocationTargetException
05-22 09:37:39.499: E/AndroidRuntime(354): at tn.pack.ordre.enregistrer.EnregistrerActivity.btn_valider(EnregistrerActivity.java:39)
05-22 09:37:39.499: E/AndroidRuntime(354): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 09:37:39.499: E/AndroidRuntime(354): at java.lang.reflect.Method.invoke(Method.java:521)
05-22 09:37:39.499: E/AndroidRuntime(354): at android.view.View$1.onClick(View.java:2026)
05-22 09:37:39.499: E/AndroidRuntime(354): ... 23 more
05-22 09:37:39.499: E/AndroidRuntime(354): Caused by: java.lang.NullPointerException
05-22 09:37:39.499: E/AndroidRuntime(354): at tn.pack.ordre.enregistrer.UserFunctions.registerUser(UserFunctions.java:60)
05-22 09:37:39.499: E/AndroidRuntime(354): ... 27 more
05-22 09:37:39.499: E/AndroidRuntime(354): ... 27 more
05-22 09:37:39.539: I/Process(52): Sending signal. PID: 354 SIG: 3
05-22 09:37:39.539: I/dalvikvm(354): threadid=7: reacting to signal 3
05-22 09:37:39.709: I/dalvikvm(354): Wrote stack trace to '/data/anr/traces.txt'
05-22 09:37:42.819: I/Process(354): Sending signal. PID: 354 SIG: 9
05-22 09:37:42.869: I/WindowManager(52): WIN DEATH: Window{44e2a0e0 tn.pack.ordre/tn.pack.ordre.enregistrer.EnregistrerActivity paused=false}
05-22 09:37:42.869: I/ActivityManager(52): Process tn.pack.ordre (pid 354) has died.
05-22 09:37:42.879: I/WindowManager(52): WIN DEATH: Window{44e00b30 tn.pack.ordre/tn.pack.ordre.enregistrer.LoginActivity paused=false}
05-22 09:37:42.909: I/ActivityManager(52): Start proc tn.pack.ordre for activity tn.pack.ordre/.enregistrer.LoginActivity: pid=363 uid=10028 gids={3003, 1015}
05-22 09:37:43.119: D/ddm-heap(363): Got feature list request
05-22 09:37:43.189: I/UsageStats(52): Unexpected resume of tn.pack.ordre while already resumed in tn.pack.ordre
05-22 09:37:43.349: D/dalvikvm(363): GC freed 719 objects / 54696 bytes in 72ms
05-22 09:37:45.359: W/InputManagerService(52): Got RemoteException sending setActive(false) notification to pid 354 uid 10028
05-22 09:37:45.741: I/ActivityManager(52): Displayed activity tn.pack.ordre/.enregistrer.LoginActivity: 2862 ms (total 2862 ms)
05-22 09:37:46.958: I/ActivityManager(52): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/.Launcher }
05-22 09:37:47.228: W/InputManagerService(52): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@44e3eaf8 (uid=10028 pid=363)
05-22 09:37:52.699: D/dalvikvm(111): GC freed 164 objects / 6600 bytes in 140ms
05-22 09:46:00.269: D/dalvikvm(52): GC freed 11198 objects / 572384 bytes in 213ms
05-22 09:56:13.229: D/dalvikvm(52): threadid=15: bogus mon 1+0>0; adjusting
05-22 10:05:11.108: D/dalvikvm(104): GC freed 10860 objects / 507696 bytes in 131ms