2016-10-08 4 views
1

저는 오래 전에 프로그래밍을 시작한 초보자입니다. 방금 ​​Java 및 Android 스튜디오를 사용하기 시작했으며 http 서버에서 데이터를 보내고받는 프로젝트 중간에 막혔습니다. Retrofit이가는 길에 있다고 들었지만 데이터를 수신 할 수 없었습니다 (솔직히, 어디서부터 시작해야할지 모르겠습니다).Retrofit을 사용하여 서버에서 데이터 수신

그러나 서버 (문자열 및 int)로 데이터를 보내고 이것이 내가 사용한 코드입니다.

내 실수와 너무 미숙 한 것에 대해 사과드립니다. 미리 감사드립니다.

개조 : https://square.github.io/retrofit/

홈페이지

public class CostumBody { 
    public String msg; 
    public int time; 

    public String getMsg() { 
     return msg; 
    } 

    public void setMsg(String msg) { 
     this.msg = msg; 
    } 

    public int getTime() { 
     return time; 
    } 

    public void setTime(int time) { 
     this.time = time; 
    } 
} 
+0

데이터를 수신 할 수 없습니다 - 오류, 시간 초과, 오류, 어떤 일이 벌어지고 있습니까? logcat에 오류가 있으면 게시하십시오. – nasch

답변

0

잘 내가 발리를 사용

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        try { 
            Button button = (Button)findViewById(R.id.button); 
            button.setOnClickListener(new View.OnClickListener() { 
                @Override 
                public void onClick(View v) { 
                    final TextView hora  = (TextView) findViewById(R.id.hora); 
                    final int sharehora = Integer.valueOf(hora.getText().toString()); 
                    final TextView missatge  = (TextView) findViewById(R.id.text); 
                    final String shareMissatge = missatge.getText().toString(); 
                    Retrofit retrofit = new Retrofit.Builder() 
                            .baseUrl("http://www.google.com/") 
                            .addConverterFactory(GsonConverterFactory.create()) 
                            .build(); 
                    CostumBody costumBody = new CostumBody(); 
                    costumBody.setMsg(shareMissatge); 
                    costumBody.setTime(sharehora); 
                    giapi service = retrofit.create(giapi.class); 
                    service.Calltomyserver(costumBody); 
    
                } 
            }); 
        } 
        catch(Exception ex){ 
            Log.e("error!", String.valueOf(ex.getMessage())); 
        } 
    } 
} 

GIAPI

public interface giapi { 
    @POST("/") 
    Call<Void> Calltomyserver (@Body CostumBody user); 
} 

custumbody, 솔직히 이유를 알고하지 않습니다. 어쨌든 불쌍한 도움!

앱 Gradle을에 발리를 설치하면 바로

컴파일을 추가 ': 도서관 AAR 1.0.0 com.mcxiaoke.volley'매우 간단하다. 데이터베이스에서 데이터를 검색하고 싶습니까? 데이터를 데이터베이스로 보냅니 까?

1 단계

설정 서버와 데이터베이스

는 데이터베이스에 원하는대로 보낼 수있는 PHP 파일을 확인하십시오. 2

단계는 데이터베이스를 JSON으로 변환합니다. 당신은 당신이 당신의 안드로이드 프로젝트를 통해 JSON을 구문 분석 할 필요가 좋은 튜토리얼 here!

3 단계

을 찾을 수 있습니다. 목록보기로 인쇄하려는 경우 데이터를 저장하려는 경우 자습서/예 here 을 찾을 수 있습니다. 목록보기로 파싱 된 행만 가져옵니다. ...

OKAY ...

은 다음 데이터베이스에이 비디오를 데이터를 전송하기 :

PHP, MySQL과 안드로이드를 연결하는 방법 - 최저 안드로이드 스튜디오 튜토리얼.

(유튜브) ...

희망이 도움이!

관련 문제