自分のサーバーでモバイルアプリケーションを作成しようとしています。私が書いたServerは、Laravelフレームワークを持つPHP上にあり、ローカルマシン上のxamppによってホストされています。私は電話を介して接続することができます - 私のローカルサイトを開くためにブラウザ。しかし、私はモバイルアプリケーションを作成し、HttpUrlConnection経由でサーバーにアクセスしたいと思います。POSTリクエスト500アンドロイドデバイスのローカルサーバー - ローカルサーバー
私のブラウザですべて正常に動作します。しかし私のPOSTリクエストは私に500コードを与えます。私は、問題はリクエストのプロパティにあると思うが、何を書くべきか分からない。
マイアンドロイド - ポスト - 要求コード:
JSONObject jsonObject = null;
URL url = null;
try {
jsonObject = new JSONObject();
jsonObject.put("name", bundle.getString("name"));
jsonObject.put("password", bundle.getString("password"));
jsonObject.put("email", bundle.getString("email"));
url = new URL("http://192.168.1.101:8081/UserAdd");
}
catch(Exception ex){
ex.printStackTrace();
}
try {
byte[] bytes = jsonObject.toString().getBytes("UTF-8");
urlConnection = (HttpURLConnection)
url.openConnection();
urlConnection.setRequestMethod("POST");
// urlConnection.setRequestProperty("Cookie", cookies);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
urlConnection.setRequestProperty("Host", "localhost:8081");
urlConnection.setRequestProperty("Content-Length", String.valueOf(bytes.length));
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
if (flag) {
urlConnection.setInstanceFollowRedirects(false);
}
OutputStream os = urlConnection.getOutputStream();
/*BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(jsonObject.toString());
writer.flush();
writer.close();*/
os.write(jsonObject.toString().getBytes());
os.flush();
os.close();
urlConnection.connect();
boolean redirect = false;
int status = urlConnection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
if (redirect) {
String newUrl = urlConnection.getHeaderField("Location");
String cookies = urlConnection.getHeaderField("Set-Cookie");
}
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
resultJson = buffer.toString();
} catch (Exception ex) {
ex.printStackTrace();
}
コンピュータブラウザからマイネットワーク要求ヘッダー。私はこのクッキーフィールドが必要だと思うが、それを見つける場所はわからない。私はとても新しいです。私はあなたの助けを求めています。
まあまあです。呼び出すPHPファイルに構文エラーがあります。 –
しかし、そのファイルとルートは、コンピュータとモバイルブラウザで動作しています。あなたの推測で私が誤解したことがありますか? @Nandan –
http://192.168.1.101:8081/UserAddはあなたのxampサーバーのローカルIPですか? –