我想将json对象发送到我的WebService并读取响应但我的代码没有用。 JSON对象已创建,但可能不会发送。 WebService未收到此对象。
我的活动:
package com.example.secapp;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.*;
import java.io.InputStream;
public class dwa extends Activity {
Context context;
TextView imie;
TextView wiek;
Button przycisk;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wynik);
context = getApplicationContext();
Bundle bundle = getIntent().getExtras();
String URL = "http://192.168.0.105:8080/2WS/test/login";
JSONObject json = new JSONObject();
HttpResponse response;
HttpClient client = new DefaultHttpClient();
try {
json.put("login",bundle.getString("login") );
json.put("password",bundle.getString("pass") );
System.out.println(json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
try {
HttpPost post = new HttpPost(URL);
StringEntity se = new StringEntity( json.toString());
post.setHeader("Accept", "application/json");
post.setEntity(se);
response = client.execute(post);
if(response!=null){
InputStream in = response.getEntity().getContent();
}
} catch(Exception e) {
e.printStackTrace();
}
imie = (TextView) findViewById(R.id.textView3);
wiek = (TextView) findViewById(R.id.textView5);
String imieString = bundle.getString("login");
String wiekString = bundle.getString("pass");
imie.setText(imieString);
wiek.setText(wiekString);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
感谢您的帮助,对不起我的英语。 :)
答案 0 :(得分:0)
如果要在应用程序中集成api,请使用改造:square.github.io/retrofit /
希望它有所帮助。