0
我寫了下面的代碼發送一個請求到服務器並從服務器獲取響應。但是,當我運行下面的代碼它不能爲我工作。我寫了這個代碼爲this link。OkHttpClient在android
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity);
Button button = (Button)findViewById(R.id.bu1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
run();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
}
}
});
}
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
Request request = new Request.Builder()
.url("http://127.0.0.1:8080/file.php")
.build();
Call call = client.newCall(request);
Response response = call.execute();
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
String text = response.body().string();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
}
什麼錯誤。請發佈一些logcat。 – Tauqir
什麼不適合你?請向我們顯示您有互聯網許可的錯誤 – bashoogzaad
?) – ligi