2013-03-11 56 views
0

我期待从android应用程序获取一些数据,将其发布到PHP脚本,然后将其写入到PostGreSQL数据库中。我有一些困难,有谁能解释为什么数据没有被传输。我不断收到很多StrictMode违规。我希望当用户点击应用上的“上传”时,整个过程就会自动完成,数据会自动写入我的PGSQL服务器。将JSONobjects从Android发布到PHP脚本

Android应用

protected void syncToWebService(final JSONObject json){   
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000); 
     HttpResponse response; 
     String httppost = "http://users.aber.ac.uk/dwd/mfb/php/jsonscript.php";  

     try 
     { 
      HttpPost post = new HttpPost(httppost); 

      Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), i); 
      ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
      bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao); 
      byte[] ba = bao.toByteArray(); 
      String ba1=Base64.encodeToString(ba, i); 

      ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 

      postParameters.add(new BasicNameValuePair("photo", ba1.toString())); 
      postParameters.add(new BasicNameValuePair("name", resultName.getText().toString())); 
      postParameters.add(new BasicNameValuePair("description", resultDescription.getText().toString())); 
      postParameters.add(new BasicNameValuePair("latitude", resultLat.getText().toString())); 
      postParameters.add(new BasicNameValuePair("longitude", resultLong.getText().toString())); 
      postParameters.add(new BasicNameValuePair("project", resultProject.getText().toString())); 
      postParameters.add(new BasicNameValuePair("owner", username)); 
      //response = CustomHttpClient.executeHttpPost(httppost, postParameters); 

      post.setEntity(new UrlEncodedFormEntity(postParameters)); 
      response = httpclient.execute(post); 

      /*Checking response variable*/ 
      if(response!=null){ 
       InputStream in = response.getEntity().getContent(); 
      } 
     } 

     catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 

PHP文件

$conn = pg_connect("dbconnection_string"); 

    $jsonString = file_get_contents('php://input'); 
    $jsonObj = json_decode($jsonString, true); 

    if(!empty($jsonObj)) { 

      try { 
        $name = jsonObj['name']; 
        $desc = jsonObj['description']; 
        $latitude = jsonObj['latitude']; 
        $longitude = jsonObj['longitude']; 
        $project = jsonObj['project']; 
        $owner = jsonObj['owner']; 
      } 

    } 
    //decode photo string 
    $photo = $_REQUEST["photo"]; 

    echo $photo; 
    $binary=base64_decode($photo); 
    header('Content-Type: bitmap; charset=utf-8'); 

    $id = pg_query("SELECT * FROM users WHERE email = $owner"); 
    $id_assoc = pg_fetch_assoc($id); 
    $id_res = $id_assoc['u_id']; 

    $res = pg_query("INSERT INTO records (photo, name, description, latitude, longitude, project, owner) VALUES ('$photo', '$name', '$desc', '$latitude', '$longitude', '$project', '$id_res'"); 

    pg_close($conn); 

任何帮助您能给将不胜感激。

+0

你在做的UI线程的网络连接?如是;那么这就是为什么你会得到'StrictMode'违规... – 2013-03-11 12:43:54

+0

在客户端没有json ... – njzk2 2013-03-11 12:49:56

+0

我在'onCreate()'方法中构建'StrictMode',但网络连接是在上面的代码不是吗? – WebDevDanno 2013-03-11 12:50:23

回答

1

改变这一点,你在jsonObj

try { 
       $name = jsonObj['name']; 
       $desc = jsonObj['description']; 
       $latitude = jsonObj['latitude']; 
       $longitude = jsonObj['longitude']; 
       $project = jsonObj['project']; 
       $owner = jsonObj['owner']; 
    } 

错过$

try { 
       $name = $jsonObj['name']; 
       $desc = $jsonObj['description']; 
       $latitude = $jsonObj['latitude']; 
       $longitude = $jsonObj['longitude']; 
       $project = $jsonObj['project']; 
       $owner = $jsonObj['owner']; 
    } 
+0

什么是尝试{}块,并忽略发生的任何异常? – 2013-03-11 12:56:29

相关问题