저는 안드로이드에서 초보자입니다. 그래서 HTTP 요청에서 JSON 구문 분석에 문제가 있습니다. 그것에 대해 간단히 설명하겠습니다. 사실 나는 로그인 버튼을 클릭하자마자 URL이 http://excoflare.com/dev2010/bb_snet/baranzan/index.php?json=json&UM_email="+sUserName+"&UM_password="+sPassword
이고, 성공한 경우 토스트에서 사용자가 패스워드로 사용자 이름과 "패스워드"로 사용자가 "이메일"을 입력 할 때 로그인을 시도하고있다. 같은 레이아웃의 "환영 사용자 이름"메시지가 도착합니다. 나는 두 개의 클래스 (I) HelloAndroid.java 및 (ii) RestJsonClient.java결과를 구문 분석하고 Toast (Android)에 결과를 표시 할 수 없습니까?
HelloAndroid.java이
public class HelloAndroid extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button login = (Button)findViewById(R.id.login_button);
login.setOnClickListener(this);
}
public void onClick(View v) {
EditText usernameEditText = (EditText) findViewById(R.id.txt_username);
EditText passwordEditText = (EditText) findViewById(R.id.txt_password);
String sUserName = usernameEditText.getText().toString();
String sPassword = passwordEditText.getText().toString();
String address = "http://excoflare.com/dev2010/bb_snet/baranzan/index.php?json=json&UM_email="+sUserName+"&UM_password="+sPassword+"";
JSONObject json = RestJsonClient.connect(address);
/* is something missing here if yes Please HELP*/
next();
}
private void next(){
Toast.makeText(HelloAndroid.this, "Welcome username", Toast.LENGTH_SHORT).show();
}
}
난 단지 올바른으로 노력하고 이제
public class RestJsonClient {
public static JSONObject connect(String url)
{
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
JSONObject json = new JSONObject();
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
json=new JSONObject(result);
instream.close();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
/**
*
* @param is
* @return String
*/
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
RestJsonClient/유효한 이메일과 비밀 번호, 지금은 내가 원하는. 제발 도와주세요. 감사합니다 여기에 많은
logcat에서 예외가 발생하는지 확인하십시오. – Dinash
'convertStreamToString' 메서드는 무엇을 반환합니까? –