내 서버에 json 데이터를 등록 할 수 있고 응답도 받고 있지만 토스트에 표시하거나 다른 활동 (주 활동)으로 보낼 수 없습니다.
응답을 문자열로 변환하고 getter를 사용하여 mainactivity에 보내려고 시도했지만 Log.i
언급 한 토스트에서도에서 볼 수 없습니다 . 내 코드는 다음과 같습니다.
class SendJsonDataToServer extends AsyncTask<String, String, String> {
String j;
@Override
protected String doInBackground(String... params) {
String JsonResponse = "";
String JsonDATA = params[0];
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
URL url = new URL("http://gstedge.com/test/invoice/api.php?param=signup");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
// is output buffer writter
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
//set headers and method
Writer writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8"));
writer.write(JsonDATA);
// json data
writer.close();
InputStream inputStream = urlConnection.getInputStream();
//input stream
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = reader.readLine()) != null)
buffer.append(inputLine + "\n");
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
JsonResponse = buffer.toString();
j= JsonResponse;
//response data
Log.i("o/p:", JsonResponse);
//send to post execute
}
catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("wtf", "Error closing stream", e);
}
}
}
return JsonResponse;
}
나는 onpostexecute에서도 응답을 얻을 수 없었기 때문에 그것을 제거하고 그것을 문자열로 변환하고 getter를 사용하여 보내고 다른 활동에서 파싱하려고 생각했습니다. log.i("o/p")
코드 에서 위에서 언급 한 응답을 볼 수 있지만 왜 전송되지 않는지 모르겠습니다.
내 주요 활동 코드는 다음과 같습니다.
public class MainActivity extends AppCompatActivity {
EditText emailview, numberview, pwview;
Button registerview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailview = (EditText) findViewById(R.id.et1);
numberview = (EditText) findViewById(R.id.et2);
pwview = (EditText) findViewById(R.id.et3);
registerview = (Button) findViewById(R.id.btn1);
}
public void hit(View v) {
String email = emailview.getText().toString();
String contact = numberview.getText().toString();
String pw = pwview.getText().toString();
JSONObject a = new JSONObject();
try {
a.put("mail", email);
a.put("num", contact);
a.put("pass", pw);
} catch (JSONException e) {
e.printStackTrace();
}
if (a.length() > 0) {
new SendJsonDataToServer().execute(String.valueOf(a));
}
SendJsonDataToServer S = new SendJsonDataToServer();
String Jr = S.getJR();
Log.i("out:",Jr);
Toast.makeText(MainActivity.this, Jr, Toast.LENGTH_LONG).show();
Intent i= new Intent(MainActivity.this,UserAreaActivity.class);
startActivity(i);
}
}
내가 본 토스트가 비어 있습니다.
설명:
두 개의 다른 개체를 만들고 있기 때문에 SendJsonDataToServer
먼저:
new SendJsonDataToServer().execute(String.valueOf(a));
둘째:
SendJsonDataToServer S = new SendJsonDataToServer();
따라서이 두 개체는 다른 메모리 할당을받습니다. 이 코드로 응답을 받으면
String Jr = S.getJR();
그러면 응답을받은 객체와 다른 객체에서 생성 된 응답을 받게됩니다. 그래서 Jr
분명히 비어있을 것입니다. 그렇기 때문에 예상 한 응답을 인쇄 할 수 없습니다.
해결책:
먼저:
실행해야합니다.
Toast.makeText(MainActivity.this, Jr, Toast.LENGTH_LONG).show();
에
@Override
protected void onPostExecute(String result) {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
당신의 방법 AsyncTask
이있다.
둘째:
이 결과를 두 번째 활동에 보내려면 onPostExecute
메서드에 이와 같은 코드를 작성해야합니다 .
@Override
protected void onPostExecute(String result) {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
Intent i = new Intent(MainActivity.this, UserAreaActivity.class);
i.putExtra("result", result);
startActivity(i);
}
이 코드 onCreate()
를UserAreaActivity
Bundle bundle = getIntent().getExtras();
String result = bundle.getString("result");
Toast.makeText(UserAreaActivity.this, result, Toast.LENGTH_LONG).show();
이제이 활동에서 결과를 사용할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다