我正在尝试使用json连接android和php以及mysql。这是我编码的一部分,即添加数据。
基于编码
private static String url_create_fixture =“http:// android_connect / create_product.php”;
如果我通过Android手机而不是模拟器测试此应用程序,那么ip地址应该是什么?
另一个问题,我在手机上测试了这个应用程序,它说process.com.example.mobile_fixtures意外停止工作。请再试一次。
这是因为错误的IP还是我的流程出了问题?
package com.example.chatapp;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import com.example.chatapp.ChatApp.Login;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Signup extends Activity{
Button btnSignup;
EditText textName;
EditText textMail;
EditText textUname;
EditText textPassword;
JSONParser jsonParser;
private ProgressDialog pDialog;
private static final String TAG_SUCCESS = "success";
private static String login_rest = "link";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
btnSignup = (Button) findViewById(R.id.join);
textName = (EditText) findViewById(R.id.name);
textMail = (EditText) findViewById(R.id.mail);
textUname = (EditText) findViewById(R.id.username);
textPassword = (EditText) findViewById(R.id.password);
btnSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Join().execute();
}
});
}
class Join extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Signup.this);
pDialog.setMessage("Join ChatApp.....");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
String uname = textUname.getText().toString();
String password = textPassword.getText().toString();
String mail = textMail.getText().toString();
String name = textName.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uname",uname));
params.add(new BasicNameValuePair("password",password));
params.add(new BasicNameValuePair("mail",mail));
params.add(new BasicNameValuePair("name",name));
params.add(new BasicNameValuePair("call_name","signup"));
System.out.println("output"+params.toString());
jsonParser = new JSONParser();
JSONObject json = jsonParser.makeHttpRequest(login_rest,
"POST", params);
System.out.println("output"+json.toString());
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
IT显示错误:
08-21 19:47:41.368: I/dalvikvm(950): threadid=3: reacting to signal 3
08-21 19:47:41.489: I/dalvikvm(950): Wrote stack traces to '/data/anr/traces.txt'
08-21 19:47:41.689: D/dalvikvm(950): GC_FOR_ALLOC freed 72K, 3% free 9117K/9347K, paused 48ms
08-21 19:47:41.739: I/dalvikvm-heap(950): Grow heap (frag case) to 17.759MB for 9216016-byte allocation
08-21 19:47:41.839: D/dalvikvm(950): GC_CONCURRENT freed 1K, 2% free 18116K/18375K, paused 3ms+13ms
08-21 19:47:41.869: I/dalvikvm(950): threadid=3: reacting to signal 3
08-21 19:47:41.879: I/dalvikvm(950): Wrote stack traces to '/data/anr/traces.txt'
08-21 19:47:42.379: I/dalvikvm(950): threadid=3: reacting to signal 3
08-21 19:47:42.389: I/dalvikvm(950): Wrote stack traces to '/data/anr/traces.txt'
08-21 19:47:42.429: D/gralloc_goldfish(950): Emulator without GPU emulation detected.
08-21 19:47:57.299: D/InputEventConsistencyVerifier(950): KeyEvent: ACTION_UP but key was not down.
08-21 19:47:57.299: D/InputEventConsistencyVerifier(950): in android.widget.EditText@41b95b10
08-21 19:47:57.299: D/InputEventConsistencyVerifier(950): 0: sent at 1193611000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_DPAD_DOWN, scanCode=108, metaState=0, flags=0x8, repeatCount=0, eventTime=1193611, downTime=1193611, deviceId=0, source=0x301 }
08-21 19:47:59.879: D/InputEventConsistencyVerifier(950): KeyEvent: ACTION_UP but key was not down.
08-21 19:47:59.879: D/InputEventConsistencyVerifier(950): in android.widget.Button@41b975a0
08-21 19:47:59.879: D/InputEventConsistencyVerifier(950): 0: sent at 1196095000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_DPAD_DOWN, scanCode=108, metaState=0, flags=0x8, repeatCount=0, eventTime=1196095, downTime=1196095, deviceId=0, source=0x301 }
08-21 19:48:00.279: I/System.out(950): output[uname=ttt, password=ttt, mail=ttt, name=tt, call_name=signup]
08-21 19:48:01.839: E/JSON Parser(950): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
08-21 19:48:01.879: W/dalvikvm(950): threadid=12: thread exiting with uncaught exception (group=0x409c01f8)
08-21 19:48:01.939: E/AndroidRuntime(950): FATAL EXCEPTION: AsyncTask #1
08-21 19:48:01.939: E/AndroidRuntime(950): java.lang.RuntimeException: An error occured while executing doInBackground()
08-21 19:48:01.939: E/AndroidRuntime(950): at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-21 19:48:01.939: E/AndroidRuntime(950): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-21 19:48:01.939: E/AndroidRuntime(950): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-21 19:48:01.939: E/AndroidRuntime(950): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-21 19:48:01.939: E/AndroidRuntime(950): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-21 19:48:01.939: E/AndroidRuntime(950): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-21 19:48:01.939: E/AndroidRuntime(950): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-21 19:48:01.939: E/AndroidRuntime(950): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-21 19:48:01.939: E/AndroidRuntime(950): at java.lang.Thread.run(Thread.java:856)
08-21 19:48:01.939: E/AndroidRuntime(950): Caused by: java.lang.NullPointerException
08-21 19:48:01.939: E/AndroidRuntime(950): at com.example.chatapp.Signup$Join.doInBackground(Signup.java:72)
08-21 19:48:01.939: E/AndroidRuntime(950): at com.example.chatapp.Signup$Join.doInBackground(Signup.java:1)
08-21 19:48:01.939: E/AndroidRuntime(950): at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-21 19:48:01.939: E/AndroidRuntime(950): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-21 19:48:01.939: E/AndroidRuntime(950): ... 5 more
08-21 19:48:02.739: I/dalvikvm(950): threadid=3: reacting to signal 3
08-21 19:48:02.779: I/dalvikvm(950): Wrote stack traces to '/data/anr/traces.txt'
08-21 19:48:03.499: E/WindowManager(950): Activity com.example.chatapp.Signup has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41bb29e0 that was originally added here
08-21 19:48:03.499: E/WindowManager(950): android.view.WindowLeaked: Activity com.example.chatapp.Signup has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41bb29e0 that was originally added here
08-21 19:48:03.499: E/WindowManager(950): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
08-21 19:48:03.499: E/WindowManager(950): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
08-21 19:48:03.499: E/WindowManager(950): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
08-21 19:48:03.499: E/WindowManager(950): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
08-21 19:48:03.499: E/WindowManager(950): at android.view.Window$LocalWindowManager.addView(Window.java:537)
08-21 19:48:03.499: E/WindowManager(950): at android.app.Dialog.show(Dialog.java:278)
08-21 19:48:03.499: E/WindowManager(950): at com.example.chatapp.Signup$Join.onPreExecute(Signup.java:55)
08-21 19:48:03.499: E/WindowManager(950): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
08-21 19:48:03.499: E/WindowManager(950): at android.os.AsyncTask.execute(AsyncTask.java:511)
08-21 19:48:03.499: E/WindowManager(950): at com.example.chatapp.Signup$1.onClick(Signup.java:43)
08-21 19:48:03.499: E/WindowManager(950): at android.view.View.performClick(View.java:3511)
08-21 19:48:03.499: E/WindowManager(950): at android.view.View$PerformClick.run(View.java:14105)
08-21 19:48:03.499: E/WindowManager(950): at android.os.Handler.handleCallback(Handler.java:605)
08-21 19:48:03.499: E/WindowManager(950): at android.os.Handler.dispatchMessage(Handler.java:92)
08-21 19:48:03.499: E/WindowManager(950): at android.os.Looper.loop(Looper.java:137)
08-21 19:48:03.499: E/WindowManager(950): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-21 19:48:03.499: E/WindowManager(950): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 19:48:03.499: E/WindowManager(950): at java.lang.reflect.Method.invoke(Method.java:511)
08-21 19:48:03.499: E/WindowManager(950): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-21 19:48:03.499: E/WindowManager(950): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-21 19:48:03.499: E/WindowManager(950): at dalvik.system.NativeStart.main(Native Method)
08-21 19:48:16.229: I/Process(950): Sending signal. PID: 950 SIG: 9
PHP编码:
<?php
require_once __DIR__ . '/db_connect.php';
/*
* Following code will create a new product row
* All product details are read from HTTP GET Request
*/
class chatappCall {
// mysql inserting a new row
function login($param)
{
$response = array();
if(isset($param['uname'])&&isset($param['password']))
{
$db = new DB_CONNECT();
$db_conn = $db->getConnected();
$result = mysqli_query($db_conn,"select * from users where uname='".$param['uname']."' and password='".$param['password']."'");
if(!empty($result)) {
// successfully inserted into database
$result = mysqli_fetch_array($result);
//$detail = array();
$response["id"] = $result["id"];
$response["name"] = $result["name"];
// success
$response["success"] = 1;
return $response;
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
return $response;
}
}
else
{
// required field is missing
$response["success"] = 0;
$response["message"] = "Invalid Username/Password.";
// echoing JSON response
return $response;
}
}
function signup($param)
{
$response = array();
if(isset($param['uname'])&&isset($param['password']))
{
$db = new DB_CONNECT();
$db_conn = $db->getConnected();
$result = mysqli_query($db_conn,"INSERT INTO `users`(`uname`, `password`, `name`, `mail`) VALUES ('".$param['uname']."','".$param['password']."','".$param['name']."','".$param['mail']."')";
if($result) {
// successfully inserted into database
$result = mysqli_fetch_array($result);
$response["success"] = 1;
$response["message"] = "Join Success Fully With ChatApp";
return $response;
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
$response["qwe"]="('".$param['uname']."','".$param['password']."','".$param['name']."','".$param['mail']." )";
return $response;
}
}
else
{
// required field is missing
$response["success"] = 0;
$response["message"] = "Error In Input.";
// echoing JSON response
return $response;
}
}
}
?>
答案 0 :(得分:0)
您的JSON解析错误可能是由于您的服务器端代码输出非JSON文本。鉴于此:
[..snip..].JSONException: Value <br of type java.lang.[..snip..]
^^^
你可能有一个PHP错误/警告,嵌入式html与JSON一起输出,破坏它。您应该尝试直接在浏览器中访问服务URL并查看返回的内容,或者在尝试解析之前在Android应用程序中记录原始响应字符串。
您的PHP代码几乎肯定也容易受到sql injection attacks的攻击。</ p>