现在我正在使用php处理RESTful API并尝试从Android连接到它 但我无法连接,因为它始终捕获IOException
这是我的Android代码(OnReg是点击注册按钮时的方法)
public void OnReg(View view)
{
str_email = et_email.getText().toString();
str_password = et_password.getText().toString();
str_fname = et_fname.getText().toString();
str_lname = et_lname.getText().toString();
type = "register";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, str_email, str_password, str_fname, str_lname);
}
这是BackgroundWorker类
public class BackgroundWorker extends AsyncTask<String, Void, String>
{
Context context;
AlertDialog alertDialog;
BackgroundWorker(Context ctx)
{
context = ctx;
}
@Override
protected String doInBackground(String... params)
{
StringBuilder result = new StringBuilder();
String type = params[0];
BufferedWriter bufferedWriter = null;
BufferedReader bufferedReader = null;
String register_url = "http://192.168.1.9/register.php";
if(type.equals("register"))
{
try
{
//Create data to send to server
JSONObject dataToSend = new JSONObject();
dataToSend.put("email", params[1]);
dataToSend.put("password", params[2]);
dataToSend.put("fname", params[3]);
dataToSend.put("lname", params[4]);
//Initialize and config request, then connect to server
URL url = new URL(register_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true); //enable output (body data)
httpURLConnection.setRequestProperty("Content-Type", "application/json"); //set header
httpURLConnection.connect();
//Write data into server
OutputStream outputStream = httpURLConnection.getOutputStream();
bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write(dataToSend.toString());
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
//Read data response from server
InputStream inputStream = httpURLConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bufferedReader.readLine()) != null)
{
result.append(line).append("\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
}
catch (IOException e)
{
return "Network Error";
}
catch (JSONException e)
{
e.printStackTrace();
}
}
return result.toString();
}
@Override
protected void onPreExecute()
{
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Status");
}
@Override
protected void onPostExecute(String result)
{
alertDialog.setMessage(result);
alertDialog.show();
}
这是我的PHP代码
<?php
$m = new MongoClient();
$db = $m -> ETESS;
$collection = $db -> members;
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$data = json_decode(file_get_contents("php://input"));
$email = $data->email;
$password = $data->password;
$fname = $data->fname;
$lname = $data->lname;
$encrypted_password = md5($password);
$document = array(
"email" => $email,
"password" => $encrypted_password,
"fname" => $fname,
"lname" => $lname
);
if($collection -> insert($document))
{
echo "Registered Successfully !";
$response["result"] = "success";
$response["message"] = "Registered Successfully !";
return json_encode($response);
}
else
{
$response["result"] = "failure";
$response["message"] = "Registered Failed";
return json_encode($response);
}
}
?>
我已经在AndroidManifest.xml中添加了这个
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
使用所有这些代码当我单击“注册按钮”时,我总是得到“网络错误”(我假设它捕获了IOException)但是当我尝试通过高级REST客户端发送POST请求时,一切都运行良好。
如果有任何错误,请检查我的代码。
由于
答案 0 :(得分:-1)
现在我尝试在我的手机中的浏览器中连接192.168.1.9/register.php,并说它
您无权访问此服务器上的/register.php。
我使用wamp作为Web服务器
由于