目标服务器无法响应!为什么在000webhost中出现此错误?

时间:2015-07-02 07:36:33

标签: android json http nullpointerexception

我使用000webhost作为示例数据库,任何人都可以帮我解决 这个?我在真实对象和模拟器上运行了这个应用程序。

我收到此错误:

07-02 07:17:27.120: W/System.err(2675): org.apache.http.NoHttpResponseException: The target server failed to respond
07-02 07:17:27.120: W/System.err(2675):     at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)

这是我的json:

public class JSONParser
{
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){

                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 {
            //Log.d("response string",json);
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

这是我的Java:

public class SignUp extends Activity {

    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();

    String name,email,password,gender,age,phone,Message,SuccessValue;


    Button SignUp,Cancel;

    EditText txtName,txtEmail,txtPassword,txtAge,txtPhone;

    RadioGroup GenderGroup;
    RadioButton GenderButton;


    private static final String url_create_user = "http://mysql14.000webhost.com/sign_up.php";
    private static final String TAG_SUCCESS = "success";
 //   private static final String TAG_MESSAGE = "message";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

        txtName = (EditText)findViewById(R.id.txtName);
        txtEmail = (EditText)findViewById(R.id.txtEmail);
        txtPassword = (EditText)findViewById(R.id.txtPassword);
        txtAge = (EditText)findViewById(R.id.txtAge);
        txtPhone = (EditText)findViewById(R.id.txtPhone);


        SignUp = (Button)findViewById(R.id.btnSignUp);
        Cancel = (Button)findViewById(R.id.btnCancel);

        addListenerOnButton();
    }

    private void addListenerOnButton() {

        // TODO Auto-generated method stub

        SignUp.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                GenderGroup = (RadioGroup)findViewById(R.id.Gender);
                int selectedId = GenderGroup.getCheckedRadioButtonId();
                GenderButton = (RadioButton)findViewById(selectedId);

                // TODO Auto-generated method stub
                 name = txtName.getText().toString();
                 email = txtEmail.getText().toString();
                 password = txtPassword.getText().toString();
                 gender = GenderButton.getText().toString();
                 age = txtAge.getText().toString();
                 phone = txtPhone.getText().toString();

    //          Toast.makeText(getApplicationContext(), name + email + password + gender + age + phone, Toast.LENGTH_SHORT).show();

                if (name.trim().equals("") || email.trim().equals("") || password.trim().equals("") || gender.trim().equals("") || age.trim().equals("") || phone.trim().equals(""))
                {   
                    Toast.makeText(getApplicationContext(), "Please enter all values", Toast.LENGTH_LONG).show();
                }
                else {
                    new CreateNewUser().execute();  
                }

            }   

    class CreateNewUser extends AsyncTask<String, String, String>{


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

                @Override
                protected String doInBackground(String... args) {
                    // TODO Auto-generated method stub
                    List<NameValuePair> params = new ArrayList<NameValuePair>();

                    params.add(new BasicNameValuePair("name", name));
                    params.add(new BasicNameValuePair("email", email));
                    params.add(new BasicNameValuePair("password", password));
                    params.add(new BasicNameValuePair("gender", gender));
                    params.add(new BasicNameValuePair("age", age));
                    params.add(new BasicNameValuePair("phone", phone));

                    Log.d("entered values", params.toString());


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


                    Log.d("Create Response", json.toString());

                    try {
                        int success = json.getInt(TAG_SUCCESS);

                        if (success == 1) 
                        {

                            SuccessValue = TAG_SUCCESS;
                            Message = json.getString("message");


                        } else {
                            // failed to create product
                            SuccessValue = "Failere";
                            Message = json.getString("message");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    return null;
                }
                protected void onPostExecute(String file_url) {
                    // dismiss the dialog once product deleted
                    pDialog.dismiss();
                    if (SuccessValue.equals(TAG_SUCCESS)){

                        Toast.makeText(SignUp.this, "Successfully created user", Toast.LENGTH_LONG).show();
                    }
                    else {
                        Toast.makeText(SignUp.this, "Some error in user creation", Toast.LENGTH_LONG).show();
                    }
                }   
            }
        }); 
    }
}

1 个答案:

答案 0 :(得分:0)

现在您的注册网址无效。
首先制作有效的网址,然后在网络浏览器中找到该网址  然后你可以测试工作网址 现在您可以看到http://mysql14.000webhost.com/sign_up.php不可用。

制作新的主机帐户并上传所有文件,然后尝试。