I am using from HttpURLConnection
to connect to a localhost
, I connect my mobile with USB tethering
to my PC(windows 10)
and then I test my app don't get me result, get me 403
.Structure of my result is JSON:
{"0":2,"1":3,"2":4,"HELLO":2,"OK":"LOLO"}
Bellow is my code :
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CatalogClient client = new CatalogClient();
client.execute("http://192.168.42.104/web/MySite/index.php");
}
}
And my AsyncTask
code is :
public class CatalogClient extends AsyncTask<String, String, JSONArray> {
String responseString;
@Override
protected JSONArray doInBackground(String... params) {
URL url;
HttpURLConnection urlConnection = null;
JSONArray response = new JSONArray();
try {
url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.connect();
int responseCode = urlConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
responseString = readStream(urlConnection.getInputStream());
Log.v("CatalogClient", responseString);
response = new JSONArray(responseString);
} else {
Log.v("CatalogClient", "Response code:" + responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null)
urlConnection.disconnect();
}
return response;
}
private String readStream(InputStream in) {
BufferedReader reader = null;
StringBuffer response = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line = "";
while ((line = reader.readLine()) != null) {
Log.i("LOG",line);
response.append(line);
}
return response.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "NULL";
}
}
And I get IP from cmd
:
NOTICE : I am using UwAmp
and my Apache port is 80
.