۱۳۹۴/۰۸/۰۲, ۱۲:۰۵ ق.ظ
(آخرین تغییر در ارسال: ۱۳۹۴/۰۸/۰۲, ۱۲:۲۳ ق.ظ توسط samaneh_62.)
سلام .من یه برنامه ی ساده نوشتم برای ارتباط بین اندروید و هاستم ولی نمیدونم چرا ارتباط برقرار نمیشه.کسی پیشنهادی داره برای حلش؟
کد Main.java :
کد Main.java :
کد پیاچپی:
package com.example.ghese;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Main extends Activity {
public static String res="";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn=(Button) findViewById(R.id.button1);
new getdata("http://samaneh62.pergig.ir/test1.php","ali").execute();
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),res,Toast.LENGTH_LONG).show();
}
});
}
کد main.xml:کد getdata.java:کد اس کیو ال فایل رابط بین اندروید و هاست:البته دسترسی به اینترنت رو در فایل منیفست هم دادم.کد Manifest:
[php]<?php
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ghese"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="20" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name="com.example.ghese.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".getdata"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
[php]<?php
خروجی باید یک پیام باشه که پسورد کاربر ali رو برگردونه ولی فقط کادر پیام ظاهر میشه
کد پیاچپی:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ghese.Main" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
کد پیاچپی:
public class getdata extends AsyncTask{
private String Link="";
private String User="" ;
public getdata(String link,String user) {
Link=link;
User=user;
}
@Override
protected String doInBackground(Object... arg0) {
try{
String data=URLEncoder.encode("username","UTF8")+"="+URLEncoder.encode(User,"UTF8");
URL mylink=new URL(Link);
URLConnection connect=mylink.openConnection();
connect.setDoOutput(true);
OutputStreamWriter wr=new OutputStreamWriter(connect.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader=new BufferedReader(new InputStreamReader(connect.getInputStream()));
StringBuilder sb=new StringBuilder();
String line=null;
while((line=reader.readLine()) !=null){
sb.append(line);
}
Main.res=sb.toString();
}catch(Exception e){
}
return "";
}
}
کد پیاچپی:
<?php
$con=mysql_connect("sql1.pergig.ir","pergi_16","pergi");
mysql_select_db("pergi_16_db1",$con);
$user=$_POST['username'];
$sqlQ="select * from users where Username='$user'" ;
$res=mysql_Query($sqlQ);
$row=mysql_fetch_array($res);
if ($row[0]){
print $row[1];
}
else
{
print "no user " ;
}
mysql_close($con);
?>
[php]<?php
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ghese"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="20" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name="com.example.ghese.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".getdata"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
[php]<?php
خروجی باید یک پیام باشه که پسورد کاربر ali رو برگردونه ولی فقط کادر پیام ظاهر میشه