凌空请求失败
我正在尝试测试凌空请求,但要保持无效结果。没有响应,未检测到错误。 我的最低SDK是23和Target SDK 31。
这是我的代码 在主要活动
public class MainActivity extends AppCompatActivity {
Button btn;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.button1);
tv=findViewById(R.id.tv1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String CT = Test_Koneksi();
tv.setText(CT);
}
});
}
private String Test_Koneksi() {
final String[] result = new String[1];
result[0]="Connection Failed";
String url = "http://192.168.81.68/query/connection_test.php";
StringRequest request = new StringRequest(Request.Method.GET,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
result[0] = response;
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
result[0]=error.getMessage();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
return result[0];
}
}
android subtest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tbj_mobile" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:usesCleartextTraffic="true"
android:allowClearUserData="true"
android:fullBackupContent="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Tbj_Mobile" >
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
上,我试图通过浏览器调用连接,而且我尝试了模拟器内部的浏览器,因此网络连接不是我猜的问题。
I'm trying to test volley request but keep getting null result. No response, no error detected.
My minimum sdk is 23 and target sdk 31.
Here is my code
On Main Activity
public class MainActivity extends AppCompatActivity {
Button btn;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.button1);
tv=findViewById(R.id.tv1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String CT = Test_Koneksi();
tv.setText(CT);
}
});
}
private String Test_Koneksi() {
final String[] result = new String[1];
result[0]="Connection Failed";
String url = "http://192.168.81.68/query/connection_test.php";
StringRequest request = new StringRequest(Request.Method.GET,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
result[0] = response;
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
result[0]=error.getMessage();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
return result[0];
}
}
Android Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tbj_mobile" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:usesCleartextTraffic="true"
android:allowClearUserData="true"
android:fullBackupContent="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Tbj_Mobile" >
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I have tried to call the connection via browser and its ok, I tried from the browser inside emulator so network connection is not the issue I guess.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论