我正在尝试创建一个注册活动,将用户详细信息发送到我的wamp服务器但是我收到一个错误,它无法找到我在PHP(registration.php)中写入的文件以将详细信息放入数据库中。我将registration.php文件保存在C:\ wamp \ www \ GolfStroke中,我的IP地址是192.168.1.11,我的设备和笔记本电脑在同一个网络上......下面是我的JSONParser类:
package com.example.______.__________;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
/**
* Created by Biko on 2/2/2016.
*/
public class JSONParser {
String charset = "UTF-8";
HttpURLConnection conn;
DataOutputStream wr;
StringBuilder result;
URL urlObj;
JSONObject jObj = null;
StringBuilder sbParams;
String paramsString;
public JSONObject makeHttpRequest(String url, String method,
HashMap<String, String> params) {
sbParams = new StringBuilder();
int i = 0;
for (String key : params.keySet()) {
try {
if (i != 0){
sbParams.append("&");
}
sbParams.append(key).append("=")
.append(URLEncoder.encode(params.get(key), charset));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
i++;
}
if (method.equals("POST")) {
// request method is POST
try {
urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept-Charset", charset);
//conn.setReadTimeout(10000);
//conn.setConnectTimeout(15000);
conn.connect();
paramsString = sbParams.toString();
wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(paramsString);
wr.flush();
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(method.equals("GET")){
// request method is GET
if (sbParams.length() != 0) {
url += "?" + sbParams.toString();
}
try {
urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept-Charset", charset);
conn.setConnectTimeout(15000);
conn.connect();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
//Receive the response from the server
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
Log.d("JSON Parser", "result: " + result.toString());
} catch (IOException e) {
e.printStackTrace();
}
conn.disconnect();
// try parse the string to a JSON object
try {
jObj = new JSONObject(result.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON Object
return jObj;
}
}
这是我的RegisterActivity类:
package com.example._______._________;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Button regBtn= (Button)findViewById(R.id.registerButton);
regBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String Name = ((EditText)findViewById(R.id.nameEditText)).getText().toString();
final String Phone = ((EditText)findViewById(R.id.phoneEditText)).getText().toString();
final String Email = ((EditText)findViewById(R.id.emailEditText)).getText().toString();
final String Password = ((EditText)findViewById(R.id.passwordEditText)).getText().toString();
class Register extends AsyncTask<String, String, JSONObject>{
JSONParser jsonParser = new JSONParser();
private ProgressDialog progressDialog;
private static final String RegURL = "http://192.168.1.11/GolfStroke/registration.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onPreExecute(){
progressDialog = new ProgressDialog(RegisterActivity.this);
progressDialog.setMessage("Registering...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(true);
progressDialog.show();
}
@Override
protected JSONObject doInBackground(String... params) {
try {
HashMap<String, String> details = new LinkedHashMap<String, String>();
details.put("Name", Name);
details.put("Phone", Phone);
details.put("Email", Email);
details.put("Password", Password);
Log.d("Hashmap", "Mapping");
JSONObject jsonObject = jsonParser.makeHttpRequest(RegURL, "POST", details);
if (jsonObject != null){
Log.d("JSON result", jsonObject.toString());
return jsonObject;
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(JSONObject jsonObject){
int success = 0;
String message = "";
if (progressDialog != null && progressDialog.isShowing()){
progressDialog.dismiss();
}
if (jsonObject != null){
Toast.makeText(RegisterActivity.this, jsonObject.toString(), Toast.LENGTH_LONG).show();
try {
success = jsonObject.getInt(TAG_SUCCESS);
message = jsonObject.getString(TAG_MESSAGE);
} catch (JSONException e) {
e.printStackTrace();
}
}
if (success == 1){
Log.d("success", message);
}else {
Log.d("Failure", message);
}
}
}new Register().execute(Name, Phone, Email, Password);
}
});
}
}
以下是我的日志:
02-02 10:50:49.753 8456-9265 / com.example.biko.golfstream D / Hashmap:Mapping 02-02 10:50:49.773 8456-8456 / com.example.biko.golfstream D / ProgressBar:updateDrawableBounds:left = 0 02-02 10:50:49.773 8456-8456 / com.example.biko.golfstream D / ProgressBar:updateDrawableBounds:top = 0 02-02 10:50:49.773 8456-8456 / com.example.biko.golfstream D / ProgressBar:updateDrawableBounds:right = 48 02-02 10:50:49.783 8456-8456 / com.example.biko.golfstream D / ProgressBar:updateDrawableBounds:bottom = 48 02-02 10:50:50.263 8456-9265 / com.example.biko.golfstream W / System.err:java.io.FileNotFoundException:http://192.168.1.11/GolfStroke/registration.php 02-02 10:50:50.263 8456-9265 / com.example.biko.golfstream W / System.err:at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186) 02-02 10:50:50.263 8456-9265 / com.example.biko.golfstream W / System.err:at com.example.biko.golfstream.JSONParser.makeHttpRequest(JSONParser.java:112) 02-02 10:50:50.263 8456-9265 / com.example.biko.golfstream W / System.err:at com.example.biko.golfstream.RegisterActivity $ 1 $ 1Register.doInBackground(RegisterActivity.java:68) 02-02 10:50:50.263 8456-9265 / com.example.biko.golfstream W / System.err:at com.example.biko.golfstream.RegisterActivity $ 1 $ 1Register.doInBackground(RegisterActivity.java:36) 02-02 10:50:50.263 8456-9265 / com.example.biko.golfstream W / System.err:at android.os.AsyncTask $ 2.call(AsyncTask.java:288) 02-02 10:50:50.263 8456-9265 / com.example.biko.golfstream W / System.err:at java.util.concurrent.FutureTask.run(FutureTask.java:237) 02-02 10:50:50.263 8456-9265 / com.example.biko.golfstream W / System.err:at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) 02-02 10:50:50.273 8456-9265 / com.example.biko.golfstream W / System.err:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 02-02 10:50:50.273 8456-9265 / com.example.biko.golfstream W / System.err:at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 02-02 10:50:50.273 8456-9265 / com.example.biko.golfstream W / System.err:at java.lang.Thread.run(Thread.java:841) 02-02 10:50:50.283 8456-9265 / com.example.biko.golfstream W / System.err:java.lang.NullPointerException 02-02 10:50:50.283 8456-9265 / com.example.biko.golfstream W / System.err:at com.example.biko.golfstream.JSONParser.makeHttpRequest(JSONParser.java:130) 02-02 10:50:50.283 8456-9265 / com.example.biko.golfstream W / System.err:at com.example.biko.golfstream.RegisterActivity $ 1 $ 1Register.doInBackground(RegisterActivity.java:68) 02-02 10:50:50.283 8456-9265 / com.example.biko.golfstream W / System.err:at com.example.biko.golfstream.RegisterActivity $ 1 $ 1Register.doInBackground(RegisterActivity.java:36) 02-02 10:50:50.283 8456-9265 / com.example.biko.golfstream W / System.err:at android.os.AsyncTask $ 2.call(AsyncTask.java:288) 02-02 10:50:50.283 8456-9265 / com.example.biko.golfstream W / System.err:at java.util.concurrent.FutureTask.run(FutureTask.java:237) 02-02 10:50:50.283 8456-9265 / com.example.biko.golfstream W / System.err:at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) 02-02 10:50:50.283 8456-9265 / com.example.biko.golfstream W / System.err:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 02-02 10:50:50.283 8456-9265 / com.example.biko.golfstream W / System.err:at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 02-02 10:50:50.283 8456-9265 / com.example.biko.golfstream W / System.err:at java.lang.Thread.run(Thread.java:841)
有谁知道我哪里出错了?非常感谢所有帮助!
答案 0 :(得分:1)
如果您使用的是本地服务器,请尝试使用10.0.2.2
IP地址,而不要使用192.168.1.11
。
http://10.0.2.2:<hostport>
和Genymotion的 http://10.0.3.2:<hostport>
!