如何使用JSON解析将数据从Android发布到PHP?

时间:2013-11-11 08:56:42

标签: android json

我想实现在php服务器上发布数据。我正在使用此代码发布数据值:

Url = insert into `user_answers`(`q_prefix`,`question_id`,`answer`) values('',,'') ;

我得到了:

  

socketException:Permission Denied

我的数据未发布到此网址。这是我的代码:

这是My Activity,包含在方法

之后
//http://192.168.1.61/jyoti/android_app/submit_answer.php
public class Gsk_Demo<NameValuePair> extends Activity
{
    ProgressDialog dialog;
    Context context;
    EditText edText_Code;
    TextView edText_ReadCode;
    Button button_Submit;
    RadioGroup rdioGroup_AnswersOption;
    RadioButton radioButton_A , radioButton_B , radioButton_C , radioButton_D ;
    String strCode;

    static final String KEY_CODE_ID = "q_id";
    static final String KEY_CODE = "q_prefix";
    static final String KEY_ANS = "answer";

    public void onCreate(Bundle svedInstanceState)
    {
        super.onCreate(svedInstanceState);
        setContentView(R.layout.demo);

        edText_Code=(EditText)findViewById(R.id.editText1_GSK_Code);
        edText_ReadCode=(TextView)findViewById(R.id.textView_ReadCode);

        button_Submit=(Button)findViewById(R.id.buttonSubmit);
        button_Submit.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0)
            {
                // TODO Auto-generated method stub

                strCode=edText_Code.getText().toString();
                create_File(strCode);
                strCode="";

                System.out.println("strCode =" +strCode);
                Log.e("strCode ","= "+strCode);
                if(edText_Code.getText().length()==0)
                { 
                    edText_Code.setError("Please Enter The Above Code!!");
                    Toast.makeText(getApplicationContext(), "File created succesfully When Click on Button",
                            Toast.LENGTH_SHORT).show();

                }

                String selectionRadioButton;

                rdioGroup_AnswersOption=(RadioGroup)findViewById(R.id.radioGroup1);
                radioButton_A=(RadioButton)findViewById(R.id.radio0);
                radioButton_B=(RadioButton)findViewById(R.id.radio1);
                radioButton_C=(RadioButton)findViewById(R.id.radio2);
                radioButton_D=(RadioButton)findViewById(R.id.radio3);

                TextView checked = (TextView) findViewById(rdioGroup_AnswersOption.getCheckedRadioButtonId());
                selectionRadioButton = checked.getText().toString();
                System.out.println("selectionRadioButton  =" +selectionRadioButton);
                Log.e("selectionRadioButton ","= "+selectionRadioButton);

                if(edText_Code.getText().length()!=0 )
                {
                    try 
                    {
                        Log.e("Answer Option's ","="+selectionRadioButton); 
                        post_code_Data(strCode,selectionRadioButton);
                    } 
                    catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }


            }
        });

    }

    @SuppressWarnings("unchecked")
    public void post_code_Data(String strCode , String selectAns) throws IOException
    {
        Log.e("strCode ","="+strCode);
        Log.e("strAns  ","="+selectAns);

        try
        {
            HttpClient httpClient=new DefaultHttpClient();
            String postUrlString="http://192.168.1.61/jyoti/android_app/submit_answer.php";
            HttpPost httpPost=new HttpPost(postUrlString.trim());

            //Add Ur Data
            List<NameValuePair> nameValuePair=new ArrayList<NameValuePair>(1);
            nameValuePair.add((NameValuePair) new BasicNameValuePair(KEY_CODE,strCode));
            nameValuePair.add((NameValuePair) new BasicNameValuePair(KEY_ANS,selectAns));

            httpPost.setEntity(new UrlEncodedFormEntity((List<? extends org.apache.http.NameValuePair>) nameValuePair));

            // Execute HTTP Post Request
            HttpResponse response=httpClient.execute(httpPost);
            InputStream inputStream=response.getEntity().getContent();
            InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
            BufferedReader bufferReader=new BufferedReader(inputStreamReader);
            StringBuilder stringBuilder=new StringBuilder();

            Log.e("",""+stringBuilder);
            String strBuffer=null;
            while((strBuffer=bufferReader.readLine()) != null)
            {
                stringBuilder.append(strBuffer);
            }

            //GSKHMPH 
            //GSKHMJD

            String strResponse1=stringBuilder.toString();

            Log.e("HttpResponse","->"+strResponse1);

            Toast.makeText(getApplicationContext(), "Data Post succesfully",
                    Toast.LENGTH_SHORT).show();

        }
        catch(ClientProtocolException cpe)
        {
            cpe.printStackTrace();
        }

    }

0 个答案:

没有答案