2012-09-12 4 views
4

app에서 PHP 서버에 등록 정보를 보내려고합니다. RegisterActivityJSONParser :런타임에 json 파싱 오류가 발생했습니다.

나는 두 개의 클래스가 있습니다. 나는이 프로그램을 실행하기 위해 노력하고있어하지만이 같은 일부 오류가 있습니다 :

RegisterActivity.java

public class RegisterActivity extends Activity { 

    private ProgressDialog pDialog; 

    JSONParser jsonParser = new JSONParser(); 
    EditText username; 
    EditText email; 
    EditText password; 
    EditText RePass; 

    private static String url_create_product = "http://oranz.co/pmdtest/index.php"; 

      private static final String TAG_SUCCESS = "sucess"; 
      @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Set View to register.xml 
     setContentView(R.layout.register); 
     Button registerscreen=(Button)findViewById(R.id.btnCntnueRegister); 
     // Listening to Login Screen link 

     email = (EditText)findViewById(R.id.reg_email); 
     password =(EditText)findViewById(R.id.reg_password); 
     username =(EditText)findViewById(R.id.reg_username); 
     RePass = (EditText)findViewById(R.id.rereg_password); 
     EditText passwords=(EditText)findViewById(R.id.reg_password); 
     passwords.setTransformationMethod(new PasswordTransformationMethod()); 
     EditText repasswords = (EditText) findViewById(R.id.rereg_password); 
     repasswords.setTransformationMethod(new PasswordTransformationMethod()); 



     registerscreen.setOnClickListener(new View.OnClickListener(){ 

      public void onClick(View arg0) { 

       Context context = getApplicationContext(); 
       int duration = Toast.LENGTH_SHORT; 
       if(email.getText().toString().equals("")&&password.getText().toString().equals("")&&username.getText().toString().equals("")&&RePass.getText().toString().equals("")) 
       {           
        Toast toast = Toast.makeText(context, "Please Enter a valed data. !", duration);  
        toast.show(); 
       } 
       else if(email.getText().toString().equals("")||password.getText().toString().equals("")||username.getText().toString().equals("")||RePass.getText().toString().equals("")) 
       { 
        Toast toast = Toast.makeText(context, "Please check any field is blank. !", duration);  
        toast.show(); 
       } 
       else if(password.getText().toString().compareTo(RePass.getText().toString())==0) 
       { 
       Intent k = new Intent(getApplicationContext(), ContinueRegister.class); 
       startActivity(k); 

       new CreateNewUser().execute(); 

       } 
       else 
       { 
        Toast toast = Toast.makeText(context, "Password matching is failed. !", duration);  
      toast.show(); 


       } 
      } 
      }); 
     TextView loginScreen = (TextView) findViewById(R.id.link_to_login); 

     loginScreen.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent m = new Intent(getApplicationContext(),LoginActivity.class); 
      startActivity(m); 

     } 
    }); 
    } 
      class CreateNewUser extends AsyncTask<String, String, String> { 

@Override 
protected void onPreExecute() { 
super.onPreExecute(); 
pDialog = new ProgressDialog(RegisterActivity.this); 
pDialog.setMessage("Creating Product.."); 
pDialog.setIndeterminate(false); 
pDialog.setCancelable(true); 
pDialog.show(); 
} 

protected String doInBackground(String... args) { 
String Username = username.getText().toString(); 
String Email = email.getText().toString(); 
String Password = password.getText().toString(); 

List<NameValuePair> params = new ArrayList<NameValuePair>(); 
params.add(new BasicNameValuePair("Username", Username)); 
params.add(new BasicNameValuePair("Email", Email)); 
params.add(new BasicNameValuePair("Password",Password)); 


// getting JSON Object 
// Note that create product url accepts POST method 

JSONObject json = jsonParser.makeHttpRequest(url_create_product, 
"POST", params); 

// check log cat fro response 
Log.d("Create Response", json.toString());     
// check for success tag 
try { 
int success = json.getInt(TAG_SUCCESS); 

if (success == 1) { 
// successfully created user 
Intent i = new Intent(getApplicationContext(),FinishSignupActivity.class); 
startActivity(i); 
// closing this screen 
finish(); 
} else { 
// failed to create user 
} 
} catch (JSONException e) { 
e.printStackTrace(); 
} 

return null; 
        } 

       /** 
        * After completing background task Dismiss the progress dialog 
        * **/ 
       protected void onPostExecute(String file_url) { 
// dismiss the dialog once done 
pDialog.dismiss(); 
       } 
      } 
} 

: 여기

Error parsing data org.json.JSONException: Value not of type java.lang.String cannot be converted to JSONObject" and "android.os.AsyncTask$3.done(AsyncTask.java:299)

내 코드입니다 JSONParser.java
:

public class JSONParser { 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 

    // constructor 
    public JSONParser() { 

    } 

    // function get json from url 
    // by making HTTP POST or GET method 
    public JSONObject makeHttpRequest(String url, String method, 
      List<NameValuePair> params) { 

     // Making HTTP request 
     try { 

      // check for request method 
      if(method == "POST"){ 
       // request method is POST 
       // defaultHttpClient 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(url); 
       httpPost.setEntity(new UrlEncodedFormEntity(params)); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 

      }else if(method == "GET"){ 
       // request method is GET 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       String paramString = URLEncodedUtils.format(params, "utf-8"); 
       url += "?" + paramString; 
       HttpGet httpGet = new HttpGet(url); 

       HttpResponse httpResponse = httpClient.execute(httpGet); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 
      }   


     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 
} 
+0

브라우저에서 브라우저를 호출 할 때 서버 (즉, 해당 URL)의 응답이 무엇인지 알 수 있습니까? – ponraj

+0

서버 측에서 응답을받지 못했습니다. –

+0

브라우저에서 호출하는 동안 응답을받지 못하면 클라이언트 측에서 어떻게 사용할 수 있습니까? 서버 측 코드를 게시 할 수 있습니까? – ponraj

답변

1

이것은 안드로이드 에뮬레이터 충돌로 인한 프로그램에서 실수가 아닙니다.

+1

나는 똑같은 문제가 있었어 ... 네 말이 맞다! – Niko

관련 문제