【问题标题】:Connecting a new Android project with parse-server-example and parse-dashboard (local)使用 parse-server-example 和 parse-dashboard(本地)连接新的 Android 项目
【发布时间】:2016-06-28 19:06:24
【问题描述】:

我正在尝试将一个新的 android 项目与 parse-server-example (本地)连接并使用 parse-dashboard 显示它。我已经成功地将 parse-server-example 与 parse-dashboard... 链接在一起(通过更改 parse-server-example/index.js 和 parse-dashboard/parse-dashboard-config.json 中的 appId 和 masterKey)。我已经安装了 mongodb 和所有东西(mongodb、parse-server-example 和 parse-dashboard 运行良好)。

但是现在当我尝试在 android 项目中初始化解析服务器时,它不会在本地解析中创建任何东西(类)。(与 parse.com 一起工作正常)。谢谢。我的代码

清单文件

<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (c) 2015-present, Parse, LLC.
  ~ All rights reserved.
  ~
  ~ This source code is licensed under the BSD-style license found in the
  ~ LICENSE file in the root directory of this source tree. An additional grant
  ~ of patent rights can be found in the PATENTS file in the same directory.
  -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.parse.starter" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:name=".StarterApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.parse.APPLICATION_ID"
            android:value="@string/parse_app_id" />
        <meta-data
            android:name="com.parse.CLIENT_KEY"
            android:value="@string/parse_client_key" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Build.gradle 文件

...

dependencies {

    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.parse.bolts:bolts-tasks:1.3.0'
    compile 'com.parse:parse-android:1.13.0'
}

MainActivity.java

package com.parse.starter;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseObject;


public class MainActivity extends AppCompatActivity {

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

    ParseAnalytics.trackAppOpenedInBackground(getIntent());


  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
      return true;
    }

    return super.onOptionsItemSelected(item);
  }
}

StarterApplication.java

package com.parse.starter;

import android.app.Application;

import com.parse.Parse;
import com.parse.ParseACL;
import com.parse.ParseObject;
import com.parse.ParseUser;


public class StarterApplication extends Application {

  @Override
  public void onCreate() {
    super.onCreate();

    // Enable Local Datastore.
    Parse.enableLocalDatastore(this);

    // Add your initialization code here
   //Parse.initialize(this);

      Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
              .applicationId("@string/parse_app_id")
              .clientKey("@string/parse_client_key")
              .server("http://localhost:1337/parse/")   // '/' important after 'parse'
              .build());

      ParseObject testObject = new ParseObject("TestObject");
      testObject.put("foo", "bar");
      testObject.saveInBackground();


      ParseUser.enableAutomaticUser();
    ParseACL defaultACL = new ParseACL();
    // Optionally enable public read access.
    // defaultACL.setPublicReadAccess(true);
    ParseACL.setDefaultACL(defaultACL, true);
  }
}

我也尝试过将对象的创建放在主要活动中

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

    ParseAnalytics.trackAppOpenedInBackground(getIntent());

    ParseObject testObject = new ParseObject("TestObject");
    testObject.put("foo", "bar");
    testObject.saveInBackground();

  }

【问题讨论】:

  • 确保删除 Android Manifest 文件中的以下行 这是因为您在 Starter Application.java 中使用自定义配置

标签: android mongodb parse-platform parse-server


【解决方案1】:

为什么你的代码中有 localhost?本地主机表示请求在本地设备上运行,您可以在服务器内部使用它,但不能在客户端设备上使用。尝试将服务器/您的计算机的 IP 地址放在那里

.server("http://localhost:1337/parse/")

【讨论】:

  • 谢谢您的回答..解决了!!
  • 你为 ParseUser.getCurrentUser() 做了什么。文档说使用 request.user 但是什么是请求对象。
  • 而不是localhost写远程设备可以远程访问的服务器的IP地址,例如10.0.0.3或192.168.1.1,你的IP地址看起来会有所不同
【解决方案2】:

在你的初始化方法中,使用 .server(API_ADDRESS) ,api地址由解析服务提供者提供。例如back4app 称之为 API 地址

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 2016-09-13
    相关资源
    最近更新 更多