0
我在將Json從應用程序發送到API時遇到問題。 API = Spring + Hibernate。 API將工作,因爲發送的Json POST程序POSTMAN被添加到數據庫中。移動應用程序不會發送JSON文件。傳輸網址充當空白的基本表單需要的表格。Android POST Json到API
public class MainActivity extends AppCompatActivity {
TextView txtStatus;
LoginButton loginButton;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
initializeControls();
loginWithFB();
}
private void initializeControls(){
callbackManager = CallbackManager.Factory.create();
txtStatus = (TextView)findViewById(R.id.txtStatus);
loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("email", "user_birthday, user_photos"));
}
private void loginWithFB(){
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.e("Success", "Login");
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name ,email, gender, birthday");
GraphRequest gr = GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject json, GraphResponse response) {
String jsonResult = String.valueOf(json);
txtStatus.setText(jsonResult);
URL url = null;
try {
url = new URL("http://pikmybeta.x3pg3pxqri.eu-central-1.elasticbeanstalk.com/pikmy/user/create");
} catch (MalformedURLException e) {
e.printStackTrace();
}
AsyncT asyncT = new AsyncT(jsonResult,url);
asyncT.execute();
}
});
gr.setParameters(parameters);
gr.executeAsync();
}
@Override
public void onCancel() {
txtStatus.setText("Cancelled");
}
@Override
public void onError(FacebookException error) {
txtStatus.setText("Error " + error.getMessage());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
。
public class AsyncT extends AsyncTask<String,Void,Void> {
String jsonResult;
URL url;
public AsyncT(String jsonResult, URL url) {
this.jsonResult = jsonResult;
this.url = url;
}
@Override
protected Void doInBackground(String... params) {
try {
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.connect();
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes(jsonResult);
wr.flush();
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
你得到什麼錯誤? –
它不顯示錯誤。在表中是「null」的一部分 – programmerJavaPL